STL的string容器使用很多,常常忘记一些函数的用法,决定边用边整理起来。
首先使用条件是include库
string的构造
- string s1(“abcdefg”); //用字符串的值初始化s1
- string s2(s1); //拷贝构造函数生成s1的副本s2
- string s3(s1,index); //将s1中从index的位置后的字符串作为s3的初值(从0开始计数位置)
- string s4(s1,index,len); //将s1中从index的位置后且长度为len的字符串作为s4的初值
- string s5(n,’a’); //用n个字符a来初始化字符串s5
- string s6(s1.begin(),s1.begin()+6); //截取s1.begin()到s1.begin()+6的区间作为s6的初值
void creatstr()
{
string s1("abcdefg");
string s2(s1);
string s3(s1,3);
string s4(s1,3,2);
string s5(6,'a');
string s6(s1.begin(),s1.begin()+6);
cout<<"s1: "<<s1<<endl<<"s2: "<<s2<<endl<<"s3: "<<s3<<endl;
cout<<"s4: "<<s4<<endl<<"s5: "<<s5<<endl<<"s6: "<<s6<<endl;
}
int main()
{
creatstr();
return 0;
}
头文件没写,上面有说明。看一下运行结果吧(VS):
string的常用操作函数
- s.empty(): 判断string s是否为空,不为空返回0(false);
- s1.size()/s1.length(): 返回string的长度/字符个数;
- s2.insert(s2.begin()+2,’3’); 在s2的第二个位置(begin()+2)插入新的字符‘3’;标准容器的插入pushback()也支持,只是只能插在最后;
- string operator+:串联字符串,左操作数需为string类型,右操作数可以是string,char,常字符串;
- s5.append(“qwer”); 将字符串(qwer)拼接在s5的后面;
- s5.clear(); 清空s5容器的所有内容,用eraser()可以删除单个元素;
void basicfun()
{
string s1("abcdefg");
string s2("qwert");
cout<<"s1.empty(): "<<s1.empty()<<endl<<"s1.size()/length() "<<s1.size()<<" "<< s1.length()<<endl;
s2.insert(s2.begin()+2,'3');
cout<<"s2-insert:"<<s2<<endl;
string s3=s1+s2;
string s4=s1+"gfedbca";
string s5=s1+"123"+"abc";
cout<<"string+的几种结果:"<<"s3: "<<s3 <<" s4 "<<s4 <<" s5 "<<s5<<endl;
s5.append("qwer");
cout<<"s5--append "<<s5<<endl;
s5.clear();
cout<<"s5--clear "<<s5<<endl;
}
int main()
{
//creatstr();
basicfun();
return 0;
}
string中字符处理函数
- s1.compare(s2); //将string s1 与 s2比较,若s1>s2,返回1;若s1小于s2,返回-1;相等返回0;
- operator== :s1==s2;// 比较字符串s1,s2是否相等,相等返回1,否则返回0;
- s2.find(‘e’);// 在s2中找到字符‘e’的位置并返回;
s2.find(‘e’,4); //从s2第4个位置开始找到字符‘e’的位置并返回;
s2.find(“rew”); //在s2中找到字符串“rew”的位置并返回;
s2.rfind(‘e’); // 在s2中反向查找字符‘e’的位置并返回;
s2.find_first_of(“eee”); //在s2中查找子串“eee”的位置返回
s2.find_first_not_of(“wsx”); // 在s2中查找第一个不在字符串“wsx”的字符位置返回; - s4.replace(5,6,”replace”); // 把s4中从位置5开始的6个字符替换为“replace”;
s4.replace(s4.begin(),s4.begin()+4,”try”); //把s4中从begin()到begin()+4的区域替换为try; - s5=s2.substr(2,4); //截取s2中位置2~4的字符赋值给s5;
s6=s2.substr(posi); //截取从posi开始的位置后所有字符赋值给s6;
void procfun()
{
string s1("qwer");
string s2("qwerrewq");
string s3("asdf");
cout<<"compare: "<<s1.compare(s2)<<" "<<s1.compare(s3)<<" "<<s2.compare(s2)<<endl;
cout<<" string == "<<(s1==s2)<<" "<<(s1==s1)<<endl;
cout<<"find results: "<<endl;
cout<<s2.find('e')<<" "<<s2.find('e',4)<<" "<<s2.find("rew")<<endl;
cout<<s2.rfind('e')<<" "<<s2.find_first_of("eee")<<" "<<s2.find_first_of("wsx")<<" "<<s2.find_first_not_of("wsx")<<endl;
string s4("test string");
s4.replace(5,6,"replace");
cout<<"replace 1 result: "<<s4<<endl;
s4.replace(s4.begin(),s4.begin()+4,"try");
cout<<"replace 2 result: "<<s4<<endl;
string s5=s2.substr(2,4);
size_t posi=s2.find('e');
string s6=s2.substr(posi);
cout<<"substr: "<<s5<<" "<<s6<<endl;
}
int main()
{
//creatstr();
//basicfun();
procfun();
return 0;
}
先到这吧,还有关于string的遍历和删除,其实就是容器的操作部分。
关于string 转char 之后再专门写一篇好了~
参考:http://blog.csdn.net/lsh_2013/article/details/46728993
http://blog.csdn.net/y990041769/article/details/8763366