CString與string、char*的區別和轉換
我們在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)中的型別,它是定義的一個類,定義在<string>標頭檔案中。裡面包含了對字串的各種常用操作,它較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定義在<afx.h>標頭檔案中。CString(typedef CStringT> CString)為Visual C++中最常用的字串類,繼承自CSimpleStringT類,主要應用在MFC和ATL程式設計中,所以使用CString時要包含afx.h檔案#include <afx.h>。
【例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時可能會遇到的一些錯誤:
編譯時會發現類似如下錯誤:
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了~~~
講明白了char*、string及CString的關係,可能有人對<string.h>、<cstring.h>、<string>
相互的轉換
既然這三種類型都可用於表示字串,但又是不同的型別,那他們如何轉換呢?可用的方法參見如下:
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】
void stringToCString()
{
//from string to CString
string s1 = "string1 to CString";
string s2 = "string2 to CString";
string s3 = "string3 to CString";
CString cstr(s1.c_str());
printCString(cstr);
CString cstr2;
cstr2.Format("%s", s2.c_str()); // string to CString
printCString(cstr2);
cstr2.Format("%s", s3.data()); // string to CString
printCString(cstr2);
//from CString to string
CString cstr3 = "CString to string3";
CString cstr4 = "CString to string4";
string str;
str=cstr3.GetBuffer(0);
cout << str << endl;
str = LPCSTR(cstr4);
cout << str << endl;
}
結果為:
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<BaseType,StringTraits>::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