对于 C,我们使用 char[]来表示字符串。
对于 C + + ,我看到了使用 std::string和 char数组的示例。
#include <iostream>
#include <string>
using namespace std;
int main () {
string name;
cout << "What's your name? ";
getline(cin, name);
cout << "Hello " << name << ".\n";
return 0;
}
#include <iostream>
using namespace std;
int main () {
char name[256];
cout << "What's your name? ";
cin.getline(name, 256);
cout << "Hello " << name << ".\n";
return 0;
}
(两个例子都改编自 http://www.cplusplus.com。)
这两种类型在 C + + 中的区别是什么? (在性能、 API 集成、优缺点等方面)