郑州品牌网站建设官网,网站空间续费多钱一年,电商网站开发语言,Wordpress网站能做seo吗我们知道在C语言里#xff0c;字符串是以\0结尾的一些字符的集合#xff0c;为了操作方便#xff0c;C标准库中提供了一些str系列的库函数#xff0c; 但是这些库函数与字符串是分离开的#xff0c;而且底层空间需要用户自己管理#xff0c;可 能还会越界访问。
但是在C…我们知道在C语言里字符串是以\0结尾的一些字符的集合为了操作方便C标准库中提供了一些str系列的库函数 但是这些库函数与字符串是分离开的而且底层空间需要用户自己管理可 能还会越界访问。
但是在C里却实现了string类这个string类大大方便了字符串的定义增添删除等操作
定义/赋值/增添/合并
int main()
{string s1(hello world);string s2hello linux; string s3s2;string s4;s4s2;string s5s1s2;string ret2 s1 我来了;return 0;
}
迭代器
顺序
假如我们想对字符串进行遍历那么就需要用迭代器
int main()
{ string s1(hello world);string::iterator it s1.begin();//begin()函数返回一个迭代器,指向字符串的第一个元素.while (it ! s1.end());//end()函数返回一个迭代器指向字符串的末尾{cout *it ;//打印it;*it a;//也可以进行修改}cout endl;return 0;
}
迭代器就类似于一个指针指向了第一个字母的地址, * 就是进行解引用
逆序
若是想对字符串进行倒序的读取
string s1(hello world);
auto rit s1.rbegin();//可以用auto 自动判断类型
while (rit ! s1.rend())
{cout *rit ;rit;
}
cout endl;
提取字符
//选定字符串开始的位置要的字符数
// source_str pos num
string s1(hello world);
string s3(s1, 0, 5);string s5(hello linuxaaaaaaaaaaaaaaaaaaa);
string s6(s5, 6);
//npos(最后一个取的数字不填入)-直接取到最后一个 (npos-1)string s9(s8.begin(), --s8.end());//也可以用区间
cout s9 endl;
读取长度
string s1(hello world);
//大小
cout s1.size() endl;
cout s1.length() endl;//容量
cout s1.capacity() endl;//clear清理数据
s1.clear();//最大容量
cout s1.max_size() endl;
ps:容量capacity不会缩小只会增大
保存空间/预定空间
string s;
s.reserve(100);//保存100个数据的空间,确定知道大概要多少空间提前开好减少扩容提高效率
改变大小
string s(hello world);
cout s.size() endl;
cout s.capacity() endl;
cout s endl;
//s.resize(13);//默认用\0进行改变容量
s.resize(13, x);//也可以修改为用x进行容量的改变
获取指定位置的字符
s.at(0);
s[0];
//二者一样,但是at访问失败会返回一个抛异常cout s2 endl;
尾插
string ss(world);
string s;
s.push_back(#);//#后加上s的内容
s.append(hello);//在其后边加上这个
cout s endl;
s.append(ss);//在s后边加上ss的内容
cout s endl;
s #;
s hello;
s ss;
cout s endl;
粘贴/拷贝
string str;
string base dasdasdasdasdasdasdasd;
str.assign(base);//将base的数据拷贝给str若str已经有数据那么就会将已有的数据进行覆盖
cout str endl;str.assign(base, 5, 10);//resource pos num
cout str endl;
查找
string s1(test.cpp);
size_t i s1.find(.);//找到.的位置,返回下标string s2 s1.substr(i);//begin..num-num决定了保留的个数,若不输入则默认到结尾
cout s2 endl;//如果有多个重复的字符呢-rfind返回最后一个出现要找在字符的位置
string s3(test.cpp.tar.zip);
size_t m s3.rfind(.);
string s4 s3.substr(m);
cout s4 endl;string s5();
特殊查找
string str(Please, replace the vowels in this sentence by mistakes.);
size_t found str.find_first_of(aeiou);//找到其中之一的字符即可
//size_t found str.find(a);
while (found ! string::npos)
{str[found] *;found str.find_first_of(aeiou, found 1);//found str.find(a, found 1);
}
cout str \n;
find和find_first_of 区别就是find是完美匹配要查找的字符串find_first_of找到其中一个符合的字符即可
除此之外string类里还有很多函数例如insert erase replace等可以查不需要强制记忆但是上述是比较经常用的按需记忆。 文章转载自: http://www.morning.rfqk.cn.gov.cn.rfqk.cn http://www.morning.ykgp.cn.gov.cn.ykgp.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.rksnk.cn.gov.cn.rksnk.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.xhqwm.cn.gov.cn.xhqwm.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.kfcz.cn.gov.cn.kfcz.cn http://www.morning.hncrc.cn.gov.cn.hncrc.cn http://www.morning.lbgsh.cn.gov.cn.lbgsh.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.rlqwz.cn.gov.cn.rlqwz.cn http://www.morning.ggqcg.cn.gov.cn.ggqcg.cn http://www.morning.nytpt.cn.gov.cn.nytpt.cn http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.mtktn.cn.gov.cn.mtktn.cn http://www.morning.lwrcg.cn.gov.cn.lwrcg.cn http://www.morning.qjdqj.cn.gov.cn.qjdqj.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.zyrcf.cn.gov.cn.zyrcf.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.jxjrm.cn.gov.cn.jxjrm.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.mkfhx.cn.gov.cn.mkfhx.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.nqwkn.cn.gov.cn.nqwkn.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.crfjj.cn.gov.cn.crfjj.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.wrlcy.cn.gov.cn.wrlcy.cn http://www.morning.mgnrc.cn.gov.cn.mgnrc.cn http://www.morning.sgrdp.cn.gov.cn.sgrdp.cn http://www.morning.mdfxn.cn.gov.cn.mdfxn.cn http://www.morning.mqlsf.cn.gov.cn.mqlsf.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.cljmx.cn.gov.cn.cljmx.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.xuejitest.com.gov.cn.xuejitest.com http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.bfcrp.cn.gov.cn.bfcrp.cn http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn http://www.morning.xwlmr.cn.gov.cn.xwlmr.cn http://www.morning.ngcbd.cn.gov.cn.ngcbd.cn http://www.morning.pndhh.cn.gov.cn.pndhh.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.nlglm.cn.gov.cn.nlglm.cn http://www.morning.nlryq.cn.gov.cn.nlryq.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.kzcz.cn.gov.cn.kzcz.cn http://www.morning.llcgz.cn.gov.cn.llcgz.cn http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.ckfqt.cn.gov.cn.ckfqt.cn http://www.morning.smggx.cn.gov.cn.smggx.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.lxctl.cn.gov.cn.lxctl.cn http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.dqxnd.cn.gov.cn.dqxnd.cn http://www.morning.wmdlp.cn.gov.cn.wmdlp.cn http://www.morning.wxwall.com.gov.cn.wxwall.com