对于 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集成、优缺点等方面而言)