#include<string>
using std::string;
简单总结一下
访问元素的方法
1.通过下标访问
略
2.通过迭代器访问
for(string::iterator it=s.begin();it!=s.end();it++)
printf("%c",*it);
常见函数解析
1.insert()
时间复杂度均为O(n)
①s1.insert(pos,s2)
在s1的pos号位置插入s2,注意是s1[pos]位置插入,而非s1[pos-1]
string s1="abcd",s2="xyz";
s1.insert(3,s2);
//s1="abcxyzd"
②s1.insert(it,it1,it2)
在s1的it位置插入一个字符串,字符串的首尾迭代器为it1、it2
s1.insert(s1.bgein()+3,s2.begin(),s2.end());
2.erase()
时间复杂度均为O(n)
①s,erase(it);
删除it位置的元素
②s.erase(s.begin(),s.end());
删除[first,last)的元素
③s.erase(pos,length);
从pos位置开始删除length个字符(s[pos],s[pos+1],......s[pos+length-1])
3.clear()
时间复杂度为O(1)
4.substr(pos,length)
从pos号位开始返回长度为length的字串,时间复杂度为O(length)
5.find()
时间复杂度为O(mn),mn分别为两个字符串长度
①s1.find(s2);
如果s2是s1字串,返回第一次出现的位置,如果不是,返回string::npos(-1或者一个很大的数,懒得记)
②s1.find(pos,s2);
从pos号位开始,其他同①
6.replace()
时间复杂度为O(s1.length)
①s1.replace(pos,length,s2);
s1从pos号位开始长度为length的字串替换为s2
②s1.replace(it1,it2,s2);
s1迭代器[it1,it2)范围内的字串替换成s2