//string.cpp ///by tsaiwn@csie.nctu.edu.tw #include #include //////// 以上要新版 C++ 才能用 !!! //////// C++ 字串 不同於 C /////////// 以下是 C 的東東 extern "C"{ #include #include } using namespace std; ////////////// int main() { char t[80]; string x("I am a string."); cout << "x=" << x << endl; cout << "do strcpy(t, x.c_str() );" << endl; strcpy(t, x.c_str() ); //轉成 C 的字串 printf("t=%s\n", t); printf(" length of t=%d\n", strlen(t)); string y(t); // 再轉成 C++ 字串看看 cout << "do string y(t); " << endl; cout << "C++ String y=" << y << endl; cout << "it's size= y.size() = " << y.size() << endl; cout << " y = y + \" Do you get it?\"; " << endl; y = y + " Do you get it?"; cout << "new y=" << y << endl; cout << "new size=" << y.size() << endl; }