voidtest_string1(){
string s1("hello world");//1.下标加[]for(size_t i =0; i < s1.size();++i){
s1[i]+=1;}for(size_t i =0; i < s1.size();++i){
cout << s1[i]<<" ";}
cout << endl;//i f m m p !x p s m e//2.迭代器
string::iterator it = s1.begin();while(it != s1.end()){*it -=1;++it;}//不能重定义it。即:string::iterator it = s1.begin();
it = s1.begin();while(it != s1.end()){
cout <<*it <<" ";++it;}
cout << endl;//h e l l o w o r l d//3.范围for,自动往后迭代,自动判断结束for(auto& e : s1)//for(char& e : s1){
e -=1;}for(auto e : s1){
cout << e <<" ";}
cout << endl;//延伸:范围for的底层是:迭代器//g d k k n v n q k c}
2.反向迭代器:rbegin() 和 rend()
voidtest_string2(){
string s1("hello world");
string::reverse_iterator rit = s1.rbegin();//auto rit = s1.rbegin();while(rit != s1.rend()){
cout <<*rit <<" ";++rit;}
cout << endl;//d l r o w o l l e h}//迭代器的意义?//对于string,无论是正着遍历,倒着遍历,下标加[]都足矣,为什么还要迭代器//对于string,下标和[]就足够好用,确实可以不用迭代器//但是如何是其他容器(数据结构),迭代器好用//迭代器的意义:所有的容器都可以使用迭代器这种方式去访问修改//例如:list,map,set是不支持下标加[]//结论://对于string要会用迭代器,但是可以不用
voidtestinsert(){
string s("hello world");
s +=' ';
s +='!!!';
cout << s << endl;//头插 效率低O(N)尽量少用
s.insert(0,1,'x');
s.insert(s.begin(),'y');
s.insert(0,"test");
cout << s << endl;//中间位置插入
s.insert(4,"xxxx");
cout << s << endl;}//hello world !//testyxhello world !//testxxxxyxhello world !
10.删除
voidtesterase(){
string s("hello world");
s.erase(0,1);
cout << s << endl;
s.erase(s.size()-1,1);
cout << s << endl;
s.erase(2,2);
cout << s << endl;//尾删也可以用 pop_back()
s.pop_back();
cout << s << endl;
s.erase(3);
cout << s << endl;}//ello world//ello worl//el worl//el wor//el
voidStoi(){int val =stoi("1234");
cout << val << endl;//1234}//stoi整形//stol长整型//stoul无符号整形//stoll long long//stoull无符号长整型//stof float//stod double//stold long double