首先說string的頭檔案
1、<string>
包裝了std的c++頭檔案
2、<string.h>
舊的c頭檔案
3、<cstring.h>
舊c頭檔案的std版本,切記,這不是cstring的頭檔案
詳見effective c++ 的第49條
再說cstring的頭檔案
注:vc9編譯環境下
1、<atlstr.h>
非mfc版本,控制台程式就用這個
2、<afxstr.h>
mfc版本,需要連結mfc的dll或靜态庫。網上很多人說要包含<afx.h>,<afx.h>包含的東西就比較多了cobject及其派生類,還有檔案類、時間類、異常類、字元串類等等(700多行的位置包含了afxstr.h),如果僅僅需要cstring的話,包含<afxstr.h>就夠了。
<afx.h>中的預處理
#ifdef _dll
#ifndef _afxdll
#error building mfc application with /md[d] (crt dll version) requires mfc shared dll version. please #define _afxdll or do not use /md[d]
#endif
<afxstr.h>中的預處理
#ifndef _afx
#error afxstr.h can only be used in mfc projects. use atlstr.h
注意上面的#error,大意就是這個頭檔案是在mfc工程裡用的。要麼改用<atlstr.h>,要麼在項目->屬性->正常裡設定為在共享的dll中使用mfc。