当前位置: 首页 > news >正文

珠海市网站建设的公司北京优化网站外包公司

珠海市网站建设的公司,北京优化网站外包公司,龙华做棋牌网站建设找哪家效益快,个人网站建设与实现文章目录1.迭代器(正向遍历)begin有两个版本2.反向迭代器(反向遍历)rbegin由两个版本3. at4. insert ——头插在pos位置前插入一个字符串在pos位置前插入n个字符在迭代器前插入一个字符5. erase从pos位置开始删除len个字符从迭代器位置开始删除6. replace——替换从pos位置开始… 文章目录1.迭代器(正向遍历)begin有两个版本2.反向迭代器(反向遍历)rbegin由两个版本3. at4. insert ——头插在pos位置前插入一个字符串在pos位置前插入n个字符在迭代器前插入一个字符5. erase从pos位置开始删除len个字符从迭代器位置开始删除6. replace——替换从pos位置开始的n个字符替换成想要的字符7. find ——查找例题 替换空格8. swap ——交换9. c_str10. substr11. getline1.迭代器(正向遍历) #includeiostream #includestring using namespace std; //迭代器 int main() {string s(hello world);string::iterator it s.begin();//遍历字符串while (it ! s.end()){cout *it ;it;}return 0; }s.begin()返回指向第一个位置的指针s.end()返回指向最后一个位置的下一个的指针 begin有两个版本 #includeiostream #includestring using namespace std; void func(const string s1) {string::iterator it s1.begin();//会报错while (it ! s1.end()){cout *it endl;it;} } int main() {string s(hello world);func(s);return 0; }当我们想要在一个函数中实现迭代器发现会报错是因为begin一共有两个版本 当函数的参数为const时需要返回const的迭代器 #includeiostream #includestring using namespace std; void func(const string s1) {string::const_iterator it s1.begin();//会报错while (it ! s1.end()){//*it 2; //由于it可以看作指针由const修饰后it指向的内容不能被修改cout *it endl;it;} } int main() {string s(hello world);func(s);return 0; }it可以看作指针由const修饰后it指向的内容不能被修改只能遍历和读容器的数据不能写 2.反向迭代器(反向遍历) #includeiostream #includestring using namespace std;int main() {//F反向迭代器string s(hello world);string::reverse_iterator rit s.rbegin();while (rit ! s.rend()){cout *rit ; //d l r o w o l l e hrit;}return 0; }s.rbegin()返回指向最后一个位置的指针s.rend()返回指向第一个位置前一个的指针 rbegin由两个版本 同样反向迭代器的rbegin也存在两个版本一个不用const和一个用const修饰的 #includeiostream #includestring using namespace std; int main() {string s(hello);string::const_reverse_iterator rit s.rbegin();while (rit ! s.rend()){//*rit 2; 会报错cout *rit endl;*rit;}return 0; }rit可以看作指针由const修饰后rit指向的内容不能被修改与it的const版本特性一致只能遍历和读容器的数据不能写 3. at 返回pos位置的字符 如果访问越界opertaor[ ]会直接报错 访问越界at会抛异常 4. insert ——头插 在pos位置前插入一个字符串 string insert (size_t pos, const string str); #includeiostream #includestring using namespace std; int main() {string s(world);s.insert(0, hello);//0位置前插入字符串cout s endl;//helloworldreturn 0; }在pos位置前插入n个字符 string insert (size_t pos, size_t n, char c); #includeiostream #includestring using namespace std; int main() {string s(world);s.insert(0,1,!);//pos位置插入1个字符!cout s endl;return 0; }在迭代器前插入一个字符 iterator insert (iterator p, char c); #includeiostream #includestring using namespace std; int main() {string s(world1);s.insert(s.begin()5, !);cout s endl;//world!1return 0; }s.begin()代表开头的位置s.begin()5代表1的位置在1之前插入字符! 5. erase 从pos位置开始删除len个字符 string erase (size_t pos 0, size_t len npos); npos代表缺省值即整数最大值 若len长度大于字符pos位置后的长度或者不给len值自动使用npos 则pos位置之后全部删除 #includeiostream #includestring using namespace std; int main() {string s(hello world);s.erase(0, 5);s.erase(2);//2位置后全部删除cout s endl;// world }0位置开始删除5个字符 从迭代器位置开始删除 iterator erase (iterator p); #includeiostream #includestring using namespace std; int main() {string s(hello world);s.erase(s.begin()1);cout s endl;//hllo world }从迭代器位置开始删除一个字符 6. replace——替换 从pos位置开始的n个字符替换成想要的字符 string replace (size_t pos, size_t len, const char* s); #includeiostream #includestring using namespace std; int main() {string s(hello world);s.replace(5, 1, %%d);cout s endl; }从下标为5的位置开始的1个字符 替换成%%d 7. find ——查找 size_t find (char c, size_t pos 0) const;查找字符,找到了返回当前pos位置的下标没有找到就返回npos(整形最大值) 例题 替换空格 将空格替换成%20 #includeiostream #includestring using namespace std; //替换空格问题 int main() {string s(i love you);int i 0;int sum 0;for (i 0; i s.size(); i){sum;}s.reserve(s.size() 2 * sum);//提前开空间避免replace扩容size_t pos s.find( );while (pos ! string::npos){s.replace(pos, 1, %20);pos s.find( , pos 3);//跳过上一次已经替换过的空格}cout s endl;return 0; }通过使用reserve提前扩容以及跳过上一次已经替换的空格进行效率提升 8. swap ——交换 void swap (string str); #includeiostream #includestring using namespace std; int main() {string s1(abc);string s2(wer);s1.swap(s2);//string类中swapcout s1 endl;cout s2 endl;swap(s1, s2);//类模板中的swapcout s1 endl;cout s2 endl;return 0; }string类中swap与类模板中的swap功能相同 但string类的swap只能针对string完成交换而类模板的swap可以对任意类型完成交换 string类的swap更高效一点直接修改两者指针的指向 修改s1指针指向原来s2指向的空间修改s2指针指向原来s1指针指向的空间 9. c_str const char*类型的指针 #includeiostream #includestring using namespace std; int main() {string s(hello world);cout s endl;//自定义类型 运算符重载cout s.c_str() endl;//返回一个const char*指针按照字符串打印 遇见\0结束return 0; }10. substr 从pos位置取len个字符的子串string substr (size_t pos 0, size_t len npos) const;从后往前查找字符 #includeiostream #includestring using namespace std; int main() {string file(test.zip.c);size_t pos file.rfind(.);//倒着查找字符.if (pos ! string::npos){string suffix file.substr(pos);cout suffix endl;}return 0; }有可能存在多个.存在所以从后往前找后缀名 11. getline 用来解决cin遇见空格停止的情况流提取默认使用空格/换行是多个值之间的分隔符 getline遇到换行结束stream getline (istream is, string str); #includeiostream #includestring using namespace std; int main() {//cins//输入 hello world 错误写法getline(cin,s);//正确写法return 0; }若输入hello worldcin会当成两个值若想把hello world当成一个整体使用getline
http://www.w-s-a.com/news/502215/

相关文章:

  • 微信网站 微信支付合肥seo排名收费
  • 织梦做的网站如何上线广东省广州市番禺区南村镇
  • 网站设计的导航栏怎么做太原有网站工程公司吗
  • 苏州虎丘区建设局网站如何在一个数据库做两个网站
  • 淘宝天猫优惠券网站建设费用腾讯邮箱企业邮箱登录
  • 深圳福田做网站公司海航科技网站建设
  • 网站降权查询wordpress更换文章背景色
  • 大型电商网站开发金融企业网站建设公司
  • 成都营销型网站建设价格化妆品品牌推广方案
  • 深圳公司手机网站制作苏州网站推广哪家好
  • 网站建设开发方式包括购买学校网站建设费计入什么科目
  • 做简单网站的框架图中小微企业查询平台
  • 哪些网站可以免费做产品推广建设建设部网站
  • 网站开发销售怎么做django做网站
  • 淘宝客网站做百度竞价万网域名怎么绑定网站
  • 建设网站找哪个公司北京知名大公司有哪些
  • 专业彩票网站开发网站流量在哪设置
  • 网站建设对应的岗位榆林做网站公司
  • 网站建设公司怎么算专业js网站分页怎么做
  • 网和网站的区别phpcms和帝国cms哪个好
  • wordpress改网站名字长沙网络营销外包
  • 宝塔怎么做第二个网站网站内容设计遵循的原则有
  • 网站违反了 google 质量指南免费ppt模版网站
  • 郑州网站建设郑州网站建设成都那家网站建设好
  • 温州网站排名优化公司如何招聘软件网站开发人员
  • 成都 网站建设公司哪家好襄阳行业网站建设
  • wordpress 调用时间做网站seo的公司哪家好
  • 手机上网站搭建网站账户系统
  • 西乡网站的建设柳州建站
  • 宁夏网站建设怎么样互联网 网站设计