(一) 概述
string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中;
String为Visual C++中最常用的字符串类,继承自CSimpleStringT类,主要应用在MFC和ATL编程中,主要数据类型有char(应用于ANSI),wchar_t(unicode),TCHAR(ANSI与unicode均可);
char*为C编程中最常用的字符串指针,一般以'/0'为结束标志。
(二) 构造
构造string是方便的,可以从几乎所有的字符串构造而来,包括CString和char*; CString次之,可以从基本的一些字符串变量构造而来,包括char*等; char*没有构造函数,仅可以赋值;
举例: char* psz = “joise”; CString cstr( psz ); string str( cstr );
(三) 运算符重载
a) operator=
string是最方便的,几乎可以直接用所有的字符串赋值,包括CString和char*; CString次之,可以直接用些基本的字符串赋值,包括char*等; char*只能由指针赋值,并且是极危险的操作,建议使用strcpy或者memcpy,而且char*在声明的时候如未赋初值建议先设为NULL,以避免野指针,令你抓狂;
举例: char *psz = NULL; psz = new char[10];
//当然,以上的直接写成char *psz = new char[10];也是一样
memset( psz, 0, 10 ); strcpy( psz, “joise” );
CString cstr; cstr = psz; string str; str = psz;
str = cstr; delete []psz;
b) operator+
string与CString差不多,可以直接与char*进行加法,但不可以相互使用+运算符,即string str = str + cstr是非法的,须转换成char*; char*没有+运算,只能使用strcat把两个指针连在一起;
举例: char* psz = “joise”; CString cstr = psz; cstr = cstr + psz;
string str = psz; str = str + psz;
strcat( psz, psz );
strcat( psz, cstr );//合法 strcat( psz, str );//非法
由此可见,CString可自动转换为const char*,而string不行
c) operator +=
string是最强大的,几乎可以与所有的字符串变量+=,包括CString和char*; CString次之,可以与基本的一些字符串变量进行+=而来,包括char*等; char*没有+=运算符,只能使用strcat把两个指针连在一起;
d) operator[]
CString最好,当越界时会抛出断言异常; string与char*下标越界结果未定义;
举例: char* psz = “joise”;
CString cstr = psz; cout << cstr[8];
string str = psz; cout << str[8]; cout << psz[8];
e) operator== 、operator!=、operator> 、operator< 、operator>= 、perator<=
CString与string之间不可以进行比较,但均可以与char*进行比较,并且比较的是值,而不是地址;
cout << ( psz == cstr ); cout << ( psz == str );
cout << ( str == psz ); cout << ( cstr == psz );//以上代码返回均为1
MSDN:
char *strcpy( char *strDestination, const char *strSource );
char *strncpy( char *strDest, const char *strSource, size_t count );
char *strcat( char *strDestination, const char *strSource );
char *strncat( char *strDest, const char *strSource, size_t count );
int strcmp( const char *string1, const char *string2 );
int strncmp( const char *string1, const char *string2, size_t count );
Parameters
strDestination
Null-terminated destination string
strSource
Null-terminated source string
basic_string::c_str
const E *c_str() const;
The member function returns a pointer to a nonmodifiable C string constructed by adding a terminating null element (E(0)) to the controlled sequence. Calling any non-const member function for *this can invalidate the pointer.
CString::Format
void Format( LPCTSTR lpszFormat, ... );
The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:
CString str = "Some Data";
str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list.
will cause unpredictable results.
CString::GetBuffer
LPTSTR GetBuffer( int nMinBufLength );
throw( CMemoryException );
Return Value
An LPTSTR pointer to the object’s (null-terminated) character buffer.
Parameters
nMinBufLength
The minimum size of the character buffer in characters. This value does not include space for a null terminator.
CString::ReleaseBuffer
void ReleaseBuffer( int nNewLength = -1 );
Use ReleaseBuffer to end use of a buffer allocated by GetBuffer.
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
1.c++中string到int的转换
在C标准库里面,使用atoi:
#include <cstdlib>
#include <string>
std::string text = "152";
int number = std::atoi( text.c_str() );
2.string 转 CString
CString.format(”%s”, string.c_str());
用c_str()确实比data()要好;
3.char 转 CString
CString.format(”%s”, char*);
4.char 转 string
string s(char *);
只能初始化,在不是初始化的地方最好还是用assign().
5.string 转 char *
char *p = string.c_str();
6.CString 转 string
string s(CString.GetBuffer());
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
7.字符串的内容转换为字符数组和C—string
(1) data(),返回没有”/0“的字符串数组
(2) c_str(),返回有”/0“的字符串数组
(3) copy()