io
//string无法用scanf和printf
string str="hello";
string str2=str;
string s3=s+s2; //字符串拼接,注意先后
string s4;
cin>>s4; //遇到空格停止读入,需要行用getline()
cout<<s4; // 格式化输入输出得用scanf和printf
//访问
str[0]; //访问第一个字符
string::iterator it; //通过迭代器访问
for(it=str.begin();it!=str.end();it++)
cout<<*it;
for(auto i;i!=str.end();i++) //c++11
cout<<*it;
//printf("%c",*t)
//getline()
string s;
getline(cin,s); //读取整行新的字符串,包括空格
cout<<s.length() ; //输出长度
//截取
//str.substr()和str.substr(a,b)
string s2=s1.substr(4) //从4到结束
string s3=s1.substr(5,3)//从5开始,三个字符
//插入
str.push_back('a'); //插入到末尾
str.insert() //插入
str.insert(index,string)
string="abc",str2="123"
str.insert(2,str2); //ab123c
//删除
str.pop_back() //删除最后的元素
str.erase() //删除单个字符
str.erase(it) // 删除it指向的字符串
str.erase(first,last) //删除first到laist之间的字符(都是迭代器)
str.erase(index,lenth) //删除下标开始后几个字符
//例如
string str (“This is an example phrase.”);
string::iterator it;
// 第(1)种用法
str.erase (10,8);
cout << str << endl; // “This is an phrase.”
// 第(2)种用法
it=str.begin()+9;
str.erase (it);
cout << str << endl; // “This is a phrase.”
// 第(3)种用法
str.erase (str.begin()+5, str.end()-7);
cout << str << endl; // "This phrase."
//清空
str.clear()
//查找
str.find()
str.find(str2) //返回str2出现的第一个位置下标,否则返回-1
string s1 = "abcdef";
string s2 = "de";
int ans = s1.find(s2) ; //在S1中查找子串S2
cout << ans << endl; //3
int ans1 = s1.find(s2, 2) ; //从S1的第二个字符开始查找子串S2
cout << ans << endl; //3
//替换
str.replace()
str.replace(index,len,str2);
strreplace(it1,it2,str2);