1.使用
#include <string>
2.声明与初始化
string str1;
string str2 = "apple";
string str3 = {"The apple tree"};
string对象大小可以自动调整,不需要在声明时指定。
3.赋值、拼接和附加
string str1;
string str2 = "test";
string str3;
str1 = str2; // 赋值
str3 = str1 + str2; //拼接
str1 += str2; //附加
4.求对象的大小
类方法:.size()
string str1 = "Apple";
str1.size(); //5
5. string类的IO
using namespace std;
string str1;
getline(cin, str1);
//一般形式的输入为
const int CSize = 30;
char str3[CSize];
int test;
cin >> str3; //读一个单词
cin.getline(str3, CSize); // 读一行
cin >> test; // 读一个数