One: standard library type string① using string to include header files
#inludde<string>using std:string;
initialization of the ②sting object
string//初始化一个空串string//吧s1赋值给s2(也就是拷贝初始化)string s2 = s1;string s3("value");//(直接初始化)string"value"string‘c‘//把s4初始化为n个字符组成的串(直接初始化)
Direct initialization and copy initialization
如果使用=号初始化一个变量,实际上执行的是拷贝初始化,编译器直接把等号右边的初始值直接拷贝到新创建的对象中去。相反如果不使用等号,则执行的就是直接初始化。
operation of the ③string object
//从键盘读取一个一个单词string s1;while (cin >> s1) { cout << s1 << endl; }
//读取一行whilestr)) { str << endl; }
str//判断字符串是不是为空str.size() //得到字符串的大小,有几个字符
related to String:: Size_type type
Because the size () function of string returns an unsigned integer, it may have unintended consequences if the unsigned number of signed symbols is mixed in an expression. So the size_type represents the unsigned integer.
Stringlen = str.size();
Comparison of String objects using < > =
Traversal of a String object
//遍历字符串,打印出一个一个字符for (auto c : str) { cout << c << endl; }
You can also access by index
if(!str.empty()){ str[0] << endl;}
C + + Standard library string