我知道一点C,现在我正在看C++。 我已经习惯了字符数组用于处理C字符串,但是当我看到C++代码,我看到有同时使用字符串型和字符数组的例子:C++中字符串和char []类型之间的区别
#include <iostream>
#include <string>
using namespace std;
int main() {
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
}
和
#include <iostream>
using namespace std;
int main() {
char name[256], title[256];
cout << "Enter your name: ";
cin.getline (name,256);
cout << "Enter your favourite movie: ";
cin.getline (title,256);
cout << name << "'s favourite movie is " << title;
return 0;
}
(这两个示例从http://www.cplusplus.com)
我想这是一个广泛提出并回答(显而易见的问题),但如果有人能告诉我在C++中处理字符串的两种方式之间的差别(性能,API i整合,每种方式都更好,...)。
谢谢。
这可有助于: [C++的char * VS的std :: string](http://stackoverflow.com/questions/801209/c-char-vs-stdstring ) – 2009-08-17 10:57:23