做公司展示网站,wordpress所有数据迁移,腾讯服务器,c2c模式类型二、string基本字符系列容器
简介#xff1a;C语言只提供了一个char类型来处理字符#xff0c;而对于字符串#xff0c;只能通过字符串数组来处理#xff0c;显得十分不方便。CSTL提供了string基本字符系列容器来处理字符串#xff0c;可以把string理解为字符串类#x…二、string基本字符系列容器
简介C语言只提供了一个char类型来处理字符而对于字符串只能通过字符串数组来处理显得十分不方便。CSTL提供了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(); 9string对象的比较 compare(); (它比对方大返回1相等返回0小于对方返回-1) 10反向排序string对象 reserve(); (使用reserve方法需要声明头文件 #include algorithm) 11string对象作为vector向量的元素类似于字符串数组 12string类型的数字化处理 13string对象与字符数组互操作 14string对象与sscanf函数 15string对象与数值相互转换
1string基本字符系列容器的一些基本操作
#include iostream
#includestring
#includestdio.h//使用C语言中的scanf函数
using namespace std;int main()
{string s; //创建一个空字符串对象s其长度为0cout s.length() ;cout endl;string s1;s1beyondwsq; //直接给字符串对象s1赋值cout s1 ;cout endl;string s2;char ss2[100];//cin ss2;scanf(%s,ss2);//scanf的输入速度要比cin快得多但是scanf是C语言的函数不支持string对象需要头文件 #includestdio.h//手动输入一个字符串到字符数组ss2里面s2ss2;//将字符数组ss2里面的元素赋值给string类型的s2cout s2 ;//输出s2这个字符串cout endl;return 0;
}2一些函数方法的使用
#include iostream
#includestring
using namespace std;int main()
{//使用 操作符从string对象尾部追加字符串string s;s s a;s s b;s s c; //不能简写成 sc; 这样的话只能输出最后一个字符cout s ; //这里若简写成 ss... 的话只能输出最后一个字符我也不知道为什么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(it1,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
#includestring
using namespace std;int main()
{string s;sbeyondwsq1014;string :: iterator it s.begin(); //定义迭代器it指向字符串对象首元素s.erase(it3); //删除下标为3的元素即第4个源0cout s endl;//输出结果为beyndwsq1014s.erase(it,it4); // 删除下标为[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
#includestring
using namespace std;int main()
{//替换string对象的字符string s;sbeyondwsq1014;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;
}5reserve反向排序string对象
#include iostream
#includestring
#includealgorithm //使用reverse函数方法需要声明头文件
using namespace std;int main()
{string s;s 10141202;reverse(s.begin(),s.end()); //从string对象s的开始到结尾这个区间内所以的元素进行反排序cout s ;//输出结果20214101return 0;
}6string类型容器的其他功能
#include iostream
#includestdio.h
#includestring
#includevector
#includealgorithm
#includestdio.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 输出结果为(12345):15string s;s 787084934;int sum0;for(int i0;is.length();i){if(s[i] 0) sum0;else if(s[i] 1) sum1;else if(s[i] 2) sum2;else if(s[i] 3) sum3;else if(s[i] 4) sum4;else if(s[i] 5) sum5;else if(s[i] 6) sum6;else if(s[i] 7) sum7;else if(s[i] 8) sum8;else if(s[i] 9) sum9;}cout sum endl;//输出结果50//string对象与字符数组互操作string s1;char ss1[100];scanf(%s,ss1);s1ss1;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;
}7string对象与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);s1sa;s2sb;s3sc;cout s1 s2 s3endl;cout Hello world! endl;return 0;
}