二、string基本字符系列容器
简介:C语言只提供了一个char类型来处理字符,而对于字符串,只能通过字符串数组来处理,显得十分不方便。C++STL提供了string基本字符系列容器来处理字符串,可以把string理解为字符串类,它提供了添加、删除、替换、查找和比较等丰富的方法。string对象的元素下标也是从0开始的。
其中vector< char >这样的向量也可以处理字符串,但其功能比不上string。向量的元素类型可以是string,如vector< string >这样的向量,实际上就类似于C语言中的字符串数组。使用string容器,需要在头文件声明, #include< string >。
函数方法总结:
1,从string对象尾部添加字符 采用 “+” 操作符
2,从string对象尾部追加字符串
❶直接采用“+”操作符
❷采用append()方法
3,给string对象插入字符 insert();
4,访问string对象的元素,一般使用下标方法随机访问string对象的元素,下标是从0开始计数的;string对象的元素是一个字符(char)
5,删除string对象的元素
❶清空一个字符串,直接给它赋值为 空字符串 即可
❷使用clear();方法删除string对象的所有元素
❸使用erase();方法删除迭代器所指的那个元素或一个区间中的所有元素。
6,返回string对象的长度
❶返回字符串的长度 length();
❷返回字符串是否为空 empty();(bool类型)
7,替换string对象的字符 replace();
8,搜索string对象的元素或子串 find();
9,string对象的比较 compare(); (它比对方大返回1,相等返回0,小于对方返回-1)
10,反向排序string对象 reserve();
(使用reserve方法需要声明头文件 #include < algorithm>)
11,string对象作为vector向量的元素,类似于字符串数组
12,string类型的数字化处理
13,string对象与字符数组互操作
14,string对象与sscanf函数
15,string对象与数值相互转换
1,string基本字符系列容器的一些基本操作
#include <iostream>
#include<string>
#include<stdio.h>//使用C语言中的scanf函数
using namespace std;int main()
{string s; //创建一个空字符串对象s,其长度为0cout << s.length() << " ";cout << endl;string s1;s1="beyondwsq"; //直接给字符串对象s1赋值cout << s1 << " ";cout << endl;string s2;char ss2[100];//cin >> ss2;scanf("%s",&ss2);//scanf的输入速度要比cin快得多,但是scanf是C语言的函数,不支持string对象,需要头文件 #include<stdio.h>//手动输入一个字符串到字符数组ss2里面s2=ss2;//将字符数组ss2里面的元素赋值给string类型的s2cout << s2 << " ";//输出s2这个字符串cout << endl;return 0;
}
2,一些函数方法的使用
#include <iostream>
#include<string>
using namespace std;int main()
{//使用 + 操作符从string对象尾部追加字符串string s;s =s + 'a';s =s + 'b';s =s + 'c'; //不能简写成 s=+'c'; 这样的话只能输出最后一个字符cout << s << " "; //这里若简写成 s=s+'...' 的话只能输出最后一个字符,我也不知道为什么???cout << endl;//输出结果:abc//使用 + 操作符从string对象尾部追加字符串string s1;s1 = s1 + "wsq";s1 = s1 + "1014";cout << s1 << " ";cout << endl;//输出结果:wsq1014//采用append()方法从string对象尾部追加字符串string s2;s2.append("yy");s2.append("1202");cout << s2 << " ";cout << endl;//输出结果:yy1210//采用insert()方法给string对象插入字符string s3;s3 = "787084934";string :: iterator it; //定义迭代器it = s3.begin(); //迭代器位置为字符串首s3.insert(it+1,'Q'); //把字符'Q'插入到第二个字符的位置,即下标为1这个位置cout << s3 << " ";cout << endl;//输出结果:7Q87084934//访问string对象的元素string s4;s4 = "beyondwsq1014";cout << s4[0] << " "; //输出string对象s4的首元素cout <<endl;cout << s4[0]-'b' << " "; //两个相同的字符相减值为0cout <<endl;//输出结果:b 0return 0;
}
3,删除string对象的元素
#include <iostream>
#include<string>
using namespace std;int main()
{string s;s="beyondwsq1014";string :: iterator it = s.begin(); //定义迭代器it,指向字符串对象首元素s.erase(it+3); //删除下标为3的元素,即第4个源0cout << s << endl;//输出结果为:beyndwsq1014s.erase(it,it+4); // 删除下标为[0~4)直接的所有元素,即 元素 beyncout << s << endl;//输出结果为:dwsq1014//s=""; //清空字符串长度s.clear(); //清空字符串长度,这两种方法效果一样cout << s.length() <<" "; //输出字符串的长度cout << s.empty() <<endl; //判断字符串是否为空,若我为空,返回1;不为空返回0//输出结果为:0 0return 0;
}
4,替换、搜索、比较string对象的元素
#include <iostream>
#include<string>
using namespace std;int main()
{//替换string对象的字符string s;s="beyondwsq1014";s.replace(3,4,"sole"); //从下标为3的元素开始,之后的连续的4个字符也就是下标为[3,6]的元素ondw,替换成“good”cout << s << endl;//输出结果为:beysolesq104//搜索string对象的元素或子串string s1;s1 = "Never give up";cout << s1.find('e') << " "; //查找第一个字符'e',返回其下标值cout << s1.find("up") <<" "; //查找第一个字符串"up",返回第一个元素(u)的下标值cout << s1.find("are") <<endl; //查找第一个字符串"are",返回其下标值,查找不到则返回4294967295//输出结果:1 11 4294967295//string对象的比较string s2;s2 = "xi ha xi ha";cout << s2.compare("xi") <<" "; //前两个元素一样,但是s2的元素多,所以s2大,返回其他多余的元素的总个数,这里除了“xi”以外,还有9个元素,故返回9cout << s2.compare("xi ha xi ha") <<" "; //相等,返回0cout << s2.compare("zz") <<endl; //x没有z大,故返回-1//输出结果:9 0 -1return 0;
}
5,reserve反向排序string对象
#include <iostream>
#include<string>
#include<algorithm> //使用reverse函数方法需要声明头文件
using namespace std;int main()
{string s;s = "10141202";reverse(s.begin(),s.end()); //从string对象s的开始到结尾这个区间内,所以的元素进行反排序cout << s << " ";//输出结果:20214101return 0;
}
6,string类型容器的其他功能
#include <iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
#include<stdio.h> //这里要用到C语言中的scanf函数
using namespace std;int main()
{vector < string > v;v.push_back("hjj");v.push_back("xgz");v.push_back("ysrwsq");cout << v[0] <<" ";cout << v[1] <<" ";cout << v[2] <<endl;//输出结果:hjj xqz ysrwsqcout << v[0][0] <<" ";cout << v[1][1] <<" ";cout << v[2][2] <<endl;//输出结果:h j rcout << v[2].length() <<endl;//输出结果:6//string类型的数字化处理//求一个整数各个位之和,例如12345 输出结果为(1+2+3+4+5):15string s;s = "787084934";int sum=0;for(int i=0;i<s.length();i++){if(s[i] == '0') sum+=0;else if(s[i] == '1') sum+=1;else if(s[i] == '2') sum+=2;else if(s[i] == '3') sum+=3;else if(s[i] == '4') sum+=4;else if(s[i] == '5') sum+=5;else if(s[i] == '6') sum+=6;else if(s[i] == '7') sum+=7;else if(s[i] == '8') sum+=8;else if(s[i] == '9') sum+=9;}cout << sum <<endl;//输出结果:50//string对象与字符数组互操作string s1;char ss1[100];scanf("%s",&ss1);s1=ss1;printf(s1.c_str());cout << endl;cout << ss1 <<endl;printf("%s",ss1);cout << endl;cout << s1 <<endl;cout << ss1 <<endl;printf("%s",ss1);cout << endl;//输出结果:若111 则输出111五次,每行一次return 0;
}
7,string对象与sscanf函数(可以把一个字符串按你需要的方式分离出子串,甚至是数字)
#include <iostream>
#include <string>
using namespace std;int main()
{string s1,s2,s3;char sa[50],sb[50],sc[50];sscanf("abc 123 sq ","%s %s %s",sa,sb,sc);s1=sa;s2=sb;s3=sc;cout << s1 <<" "<<s2 <<" "<<s3<<endl;cout << "Hello world!" << endl;return 0;
}