💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
我们在C++的开发中经常会碰到string、char*以及CString,这三种都表示字符串类型,有很多相似又不同的地方,常常让人混淆。下面详细介绍这三者的区别、联系和转换: ## 各自的区别 ### char*: char*是一个指向字符的指针,是一个内置类型。可以指向一个字符,也可以表示字符数组的首地址(首字符的地址)。我们更多的时候是用的它的第二的功能,来表示一个字符串,功能与字符串数组char ch[n]一样,表示字符串时,最后有一个 '\0'结束符作为字符串的结束标志。 【例1】 ~~~ #include <iostream> using namespace std; void testCharArray() { char ch1[12] = "Hello Wrold"; //这里只能ch1[12],ch1[11]编译不通过,提示array bounds overflow char *pch1 , *pch2 = "string"; char *pch3, *pch4; pch3 = &ch1[2]; //ch1[2]的地址赋给pch3 char ch = 'c'; pch4 = &ch; pch1= ch1; cout << ch1 << endl; //输出ch1[0]到\0之前的所有字符 cout << pch1 << endl; //输出ch1[0]到\0之前的所有字符 cout << pch2 << endl; //输出ch1[0]到\0之前的所有字符 cout << pch3 << endl; //输出ch1[2]到\0之前的所有字符 cout << *pch3 << endl; //解引用pch3输出pch3指向的字符 cout << *pch4 << endl; //解引用pch4输出pch4指向的字符 } ~~~ **结果为:** Hello Wrold Hello Wrold string llo Wrold l C ### string: string是C++标准库(STL)中的类型,它是定义的一个类,定义在头文件中。里面包含了对字符串的各种常用操作,它较char*的优势是内容可以动态拓展,以及对字符串操作的方便快捷,用+号进行字符串的连接是最常用的操作。 【例2】 ~~~ #include <string> void testString() { string s1 = "this"; string s2 = string(" is"); string s3, s4; s3 = string(" a").append("string."); s4 = s1 + s2 + s3; cout << s1 << endl; cout << s2 << endl; cout << s3 << endl; cout << s4 << endl; cout << s4.size() << endl; s4.insert(s4.end()-7, 1, ' '); cout << s4 << endl; } ~~~ **结果为:** this  is  astring. this is astring. 16 this is a string. ### CString CString常用于MFC编程中,是属于MFC的类,如从对话框中利用GetWindowText得到的字符串就是CString类型,CString定义在头文件中。CString(typedef CStringT> CString)为Visual C++中最常用的字符串类,继承自CSimpleStringT类,主要应用在MFC和ATL编程中,所以使用CString时要包含afx.h文件#include 。 【例3】 ~~~ #include <afx.h> //因为CString不是标准C++库定义的类型,没有对<<运算符进行重载, //所以不能通过cout<<cstr来输出内容,只能自己先定义一个方法。 void printCString(const CString &cstr); void testCString() { char *ch = "Hello"; string s = "Wrold"; CString cstr1(ch), cstr2(s.c_str()), cstr3("Program"); printCString(cstr1); printCString(cstr2); printCString(cstr3); CString cstr4, cstr5; cstr4 = cstr1 + cstr2 + cstr3; cstr5 = cstr1 + " " + cstr2 + " " + cstr3; printCString(cstr4); printCString(cstr5); } void printCString(const CString &cstr) { int n = cstr.GetLength(); for(int i=0; i<n; i++) { printf("%c", cstr[i]); } printf("\n"); } ~~~ **结果为:** Hello Wrold Program HelloWroldProgram Hello Wrold Program 更多关于CString的用法请参考:[http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html](http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html)   **使用CString时可能会遇到的一些错误:**   编译时会发现类似如下错误:     Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] C:\Program Files (x86)\Microsoft Visual Studio 8\VC\ce\atlmfc\include\AFX.h 24 **解决方法:** (注:我用的开发环境是VS2010,其它VS的环境类似操作) **方法1**:这里错误提示的意思是缺少_AFXDLL这个宏,因此在Project——>property中,C/C++里面Preprocessor(预编译),加入_AFXDLL这个宏,OK搞定!! **方法2**:对着你的项目点击右键,依次选择:属性、配置属性、常规,然后右边有个“项目默认值”,下面有个MFC的使用,选择“在共享 DLL 中使用 MFC”,就OK了~~~ 参考文章:[http://blog.csdn.net/ahjxly/article/details/8465209](http://blog.csdn.net/ahjxly/article/details/8465209)                          [http://blog.csdn.net/zhoxier/article/details/7929920](http://blog.csdn.net/zhoxier/article/details/7929920)   讲明白了char*、string及CString的关系,可能有人对、、这几个头文件又糊涂了,由于篇幅的原因,这部分的内容将在下一节进行说明,欢迎阅读:《[ 与、的区别](http://blog.csdn.net/luoweifu/article/details/20242307)》   ## 相互的转换 既然这三种类型都可用于表示字符串,但又是不同的类型,那他们如何转换呢?可用的方法参见如下: ### char*与string的转换 【例4】 ~~~ void pCharToString() { //from char* to string char * ch = "hello world"; string s1 = ch; //直接初始化或赋值 string s2(ch), s3; s3 = string(ch); cout << s1 << endl; cout << s2 << endl; cout << s3 << endl; //from string to char* string str = string("string is commonly used."); /************************************************************************* 其实没有很大的必要将string转换成char*,因为string可以直接当成字符数组来使用, 即通过下标来访问字符元素,如str[1]表示第1个字符't' **************************************************************************/ const char *ch1 = str.c_str(); cout << ch1 << endl; } ~~~ **结果为:** hello world hello world hello world string is commonly used. ### char*与CString 【例5】 ~~~ void pCharToCString() { //from char* to CString char *ch = "char pointer."; CString cStr1 = ch; CString cStr2 = CString(ch); printCString(cStr1); printCString(cStr2); //from CString to char* CString cstr = "CString"; char* chs=cstr.getbuffer(0);//此方法在VS2010下编译不通过,原因见【例6】 cout << chs << endl; } ~~~ **结果为:** char pointer. char pointer. CString ## string与CString 【例6】 **结果为:** string1 to CString string2 to CString string3 to CString CString to string3 CString to string4 c_str()和data()区别是:前者返回带'/0'的字符串,后者则返回不带'/0'的字符串. 在VS2010环境下,cstr2.Format("%s", s2.c_str());cstr2.Format("%s", s3.data());及str=cstr3.GetBuffer(0);str = LPCSTR(cstr4); 可能会编不过,会报类似error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *'的错误。这是因为你的工程的字符集不是多字节字符集,将你的工程属性设置为多字节字符集即可,方法是:右键点击你的工程,选择Properties\Configurations Properties\General,在右侧的Project Defaults下的Character Set选择Use Multi-Byte Character Set。 ## 总结 从灵活度来说,string最灵活易用,其次是CString,char*的拓展性和灵活性比较差。 一般来说在基于标准库开发时用string,在在MFC和ATL编程时用CString。 CString、string之间的转换还有其它的一些方向,但基本上都是通过char*作为桥梁,因为char*即可以方便地转换成string,也可以方便地转换成CString。 更多CString的用法也可参考以下链接,他们写的更详细,我就不再重复了。 http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html http://blog.csdn.net/lewutian/article/details/6787024 欢迎加入"C/C++梦之队" 学习群:226157456