C++初始化字符串的方式很靈活:
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string s1; //初始化為空字符串
string s2("hello"); //初始化為一個字符串字面副本
string s3(s2); //將s3初始化為一個s2的副本
string s4="hi jiajia"; //初始化為一個字符串字面副本
string s5(15,'A');
cout<<s1<<endl<<s2<<endl<<s3<<endl<<s4<<endl<<s5<<endl;
system("pause");
return 0;
}
注意:由于歷史原因以及為了與C語言兼容,字符串字面值與標準庫string類型不是同一種類型。