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

上海移动网站开发大数据查询官网

上海移动网站开发,大数据查询官网,pvtc2c平台,wordpress分类详细信息字符串操作看似简单#xff0c;其实非常重要#xff0c;不注意的话#xff0c;经常出现代码运行结果和自己想要的不一致#xff0c;甚至崩溃。本文总结了一些构建string对象方法、修改string对象的方法、string类型的操作函数、string类型的查找、string对象的比较。1 构建… 字符串操作看似简单其实非常重要不注意的话经常出现代码运行结果和自己想要的不一致甚至崩溃。本文总结了一些构建string对象方法、修改string对象的方法、string类型的操作函数、string类型的查找、string对象的比较。1 构建string对象方法 首先为了在我们的程序中使用string类型我们必须包含头文件 。如下#include 声明一个字符串变量很简单string Str;这样我们就声明了一个字符串变量但既然是一个类就有构造函数和析构函数。上面的声明没有传入参数所以就直接使用了string的默认的构造函数这个函数所作的就是把Str初始化为一个空字符串。String类的构造函数和析构函数如下String类函数说明string s;生成一个空字符串sstring s(s2);拷贝构造函数 生成s2的复制品string s(value);用字符串value初始化sstring s(b,e);以区间b,e内的字符作为字符串s的初值string s(cp,n);取字符数组前n个字符作初值string s(s2,pos2);将字符串s2始于位置pos2部分当作字符串的初值string s(s2,pos1,len);将字符串s2内始于pos1且长度最多len的部分作为字符串的初值s.~string();销毁所有字符释放内存代码实例:#include #include using namespace std;//20200527 测试字符串操作int main(){ string s1; cout string s2(10,f); cout string s3(s2); cout //只是构造的时候拷贝s2修改其中一个不会影响另一个s3输出位ffffffffff string s4(s3.begin(),s3.begin()(s3.size())/2); //定义s4用迭代器做参数从第一个迭代器s3.begin() cout char *cp Hello; //最后有空字符/0 char c_array[] world!!!!; //最后有空字符/0 char no_null[] {H,e,l,l,0}; //最后没有空字符/0不算C语言字符串只是字符数组 string ss1(cp); cout string ss2(c_array,5); cout string ss3(c_array5,4); cout //string ss4(no_null);//用字符数组为ss4赋值因为找不到/0,不知道拷贝几个会出错 string ss5(no_null,2); //这次取2个就知道什么时候结束不会出错 cout s1 Hello; cout s1 endl; //s1输出Hello string s6(s1,2); cout s6 endl; //用s1初始化s62表示字符下标从第二个字符开始到最后s6为llo string s7(s1,0,2); cout s7 endl; //从s10开始取2个s7为He string s8(s1,0,8); cout s8 endl; //从s1的第一个开始取8个不够8个就结束s8为Hello return 0;}** 运行结果**2 修改string对象的方法 与容器共有的 string 操作:与容器共有的 string 操作方法说明s.insert(p,t);在迭代器 p 指向的元素之前插入一个值为 t 的新元素,返回指向新插入元素的迭代器s.insert(p,n,t);在迭代器 p 指向的元素之前插入 n 个值为 t 的新元素s.insert(p,b,e);在迭代器 p 指向的元素之前插入迭代器 b 和 e 标记范围内所有的元素。返回 voids.assign(b,e);在迭代器 b 和 e 标记范围内的元素替换 s。string类型返回 s容器类型返回 voids.assign(n,t);用值为 t 的 n 个副本替换 s。对于 string 类型该操作返回 s对于容器类型则返回 voids.erase(p);删除迭代器 p 指向的元素。返回一个迭代器指向被 删除元素后面的元素s.erase(b,e);删除迭代器 b 和 e 标记范围内所有的元素。返回一个迭代器指向被删除元素段后面的第一个元素代码实例#include #include using namespace std;//2020.05.27 测试字符串操作int main(){ string s(hello); string s2(abcdef); string::iterator p s.begin(); //迭代器p s.insert(p,A); //在迭代器p指向的s开始之前插入A cout s endl; //s为Ahello s.insert(p,3,B); //p指向返回的Ahello的A处在A之前插入3个B cout s endl; //s为BBBAhello string::iterator b s2.begin(); //迭代器b string::iterator e s2.end(); //迭代器e //p s.begin(); //p指向s s.insert(p,b,e); //在p指向的s之前插入b和e迭代器范围内的元素abcdef cout s endl; //s为abcdefBBBAhello s hello; cout s endl; //s为hello s.assign(b,e); //s所有的元素倍替换为b到e之间的元素b与e之间为s2 cout s endl; //s为abcdef s.assign(8,K); cout s endl; //s为KKKKKKKK p s2.begin(); //迭代器p指向s2的a s2.erase(p); //删除迭代器p指向的元素a cout s2 endl; //s2为bcdef p s2.begin(); //a被删除p指向b p; //指向c p; //指向d string::iterator p2 s2.end(); //p2迭代器指向f p2--; //指向e s2.erase(p,p2); //删除p指向的d和p2指向的e之间的元素 cout s2 endl; //s2为bcf return 0;}运行结果运行结果string 类型特有的版本string以数组的形式存储可以用数组的下标进行修改操作string 修改操作方法说明s.insert(pos,n,c);在下标 pos 的元素之前插入 n 个字符 cs.insert(pos,s2);在下标 pos 的元素之前插入 string 对象 s2s.insert(pos,s2,pos2,len);在下标为 pos 的元素之前插入 s2 中从下标   pos2 开始的 len 个字符s.insert(pos,cp,len);在下标为 pos 打元素之前插入 cp 所指向数组的前len 个字符s.insert(pos,cp);在下标为 pos 的元素之前插入 cp 所指向的以空字符结束的字符串副本s.assign(s2);用 s2 的副本替换 ss.assign(s2,pos2,len);用 s2 中从下标 pos2 开始的 len 个字符替换 ss.assign(cp,len);用 cp 所指向数组的前 len 个字符副本替换 ss.assign(cp);用 cp 所指向的以空字符结束的字符串替换 ss.erase(pos,len);删除从下标 pos 开始的 len 个字符代码实例#include #include using namespace std;//2020.05。27 测试字符串操作int main(){ string s(hello); string s2(abc); s.insert(0,3,A); //在s下标是0之前插入3个A cout s endl; //s为AAAhello s.insert(5,s2); //在AAAhello下标是5的元素之前插入abc cout s endl; //s为AAAheabcllo s2 123456; s.insert(0,s2,2,3); //在s的下标是0之前插入s2下标为2开始往后的3个元素345 cout s endl; //s为345AAAheabcllo char *cp Stately plup Buck; s.assign (cp,7); cout s endl; //s为Stately s.assign(cp); //没有长度默认是拷贝全部 cout s endl; //s为Stately plup Buck s hello; s.insert (0,cp,7); cout s s.insert(0,cp); cout s s hello; s2 abcdef; s.assign(s2,2,3); //s2中下标为2开始3个元素赋值给s cout s s.assign(s2); cout s s.erase (2,3); //从下标为2开始删除s中的3个元素 cout s s 123456789; s.erase(s.size()-5,5); //删除s中后5个 cout s s.insert(s.size(),5,!); //在s下标为s.size()处插入5个 cout s s abc; s.erase(0,1).insert(0,A); //先从下标为0之前删除一个a为bc再插入A cout s s abc; s[0] A; //用数组的方式处理 cout s return 0;}运行结果运行结果3 适合string类型操作的函数 substr()主要功能是复制子字符串要求从指定位置开始并具有指定的长度。append() 方法在被选元素的结尾(仍然在内部)插入指定内容。提示如需在被选元素的开头插入内容请使用prepend()方法。replace() 该函数返回一个字符串其中指定的字符串已经被替换为另一字符串并且替换的次数也可以指定。代码实例:#include #include using namespace std;//2020.05.27 测试字符串操作int main(){ string s(Hello world); string s2 s.substr(6,5); //从第6个开始取5个 cout s2 endl ; //s2为world s2 s.substr(6); //从第6个开始取拷贝所有的 cout s2 endl ; //s2为world s2 s.substr(6); //s2拷贝s的全部相当于s2s cout s2 endl ; //s2为Hello world s C Primer; s.append( 3rd Ed); //再s最后添加3rd Ed cout s endl ; //s为C Primer 3rd Ed s C Primer; s.insert(s.size(), 3rd Ed); //最后插入 cout s endl ; //s为C Primer 3rd Ed s.replace(11,3,4th); //下标11开始3个替换4th cout s endl ; //s为C Primer 4th Ed s.replace(11,3,Fourth); //下标11开始3个替换Fourth cout s endl ; //s为C Primer Fourth Ed s C Primer 3rd Ed; //replace相当于先删除后插入 s.erase (11,3); //删除3rd s.insert(11,Fourth); //插入Fourth cout s endl ; //s为C Primer Fourth Ed return 0;}运行结果运行结果4 string类型的查找 查找函数说明s.find( args);在 s 中查找 args 的第一次出现s.rfind( args);在 s 中查找 args 的最后一次出现s.find_first_of( args);在 s 中查找 args 的任意字符的第一次出现s.find_last_of( args) ;在 s 中查找 args 的任意字符的最后一次出现s.find_first_not_of( args);在 s 中查找第一个不属于 args 的字符s.find_last_not_of( args);在 s 中查找最后一个不属于 args 的字符代码实例#include #include using namespace std;//2020.05.27 测试字符串操作int main(){ string name(AnnaBelle); string::size_type pos1 name.find(Bell); cout pos1 endl; //返回下标4如果没找到返回npos if(pos1 string::npos) cout 没找到 endl; else cout 找到了下标 pos1 name 2sn3; string numerics(0123456789); string::size_type pos name.find_first_of(numerics); //在2sn3中查找0123456789中任意一个第一次出现 if(pos string::npos) cout 没找到 endl; else cout 找到了下标 pos //其他类型的查找这里就不举例子了 return 0;}运行结果运行结果5 string对象的比较 string对象比较函数compare用法说明str1.compare(str2);如果相等则输出为0str1str2输出大于0否则输出小于0str1.compare(m,n,str2);str1的子串(从索引m开始包含n个字符)与str2进行比较str1.compare(m,n,str2,m,n);str1的子串(从索引m开始包含n个字符)与str2的子串(从索引m开始包含n个字符)进行比较代码实例:#include #include #include using std::cout;using std::endl;using std::cin;using std::string;int main(void){ string str1hi,test,hello; string str2hi,test; //字符串比较 if(str1.compare(str2)0) printf(str1str2\n); else if(str1.compare(str2)0) printf(str1 else printf(str1str2\n); //str1的子串(从索引3开始包含4个字符)与str2进行比较 if(str1.compare(3,4,str2)0) printf(str1的指定子串等于str2\n); else printf(str1的指定子串不等于str2\n); //str1指定子串与str2的指定子串进行比较 if(str1.compare(3,4,str2,3,4)0) printf(str1的指定子串等于str2的指定子串\n); else printf(str1的指定子串不等于str2的指定子串\n); //str1指定子串与字符串的前n个字符进行比较 if(str1.compare(0,2,hi,hello,2)0) printf(str1的指定子串等于指定字符串的前2个字符组成的子串\n); else printf(str1的指定子串不等于指定字符串的前2个字符组成的子串\n); return 0;}运行结果运行结果本文授权转载自公众号“C语言与CPP编程”作者自成一派123-END-推荐阅读【01】C语言十大经典排序算法(动态演示代码值得收藏)【02】C语言、嵌入式中几个非常实用的宏技巧【03】C语言最全入门笔记【04】绝对能检测你C语言基础水平的5个面试题【05】C语言为何不会过时你需要掌握多少种语言免责声明整理文章为传播相关技术版权归原作者所有如有侵权请联系删除
http://www.yutouwan.com/news/287298/

相关文章:

  • 镇江做网站的湖北黄石网站群建设
  • 怎样建一个免费网站互动网站建设公司
  • 化妆品网站设计模板网站换服务器对网站排名有影响吗
  • 个人 备案 多个网站网站降权查询
  • 校园超市网站开发python编写网页
  • 做ppt模板网站有哪些php 商务网站开发实战
  • 英文网站开发哪家好设计页面教案
  • 百度网站开发业务坂田网站建设流程
  • 做微信平台网站中国石油销售公司网站建设
  • 网站footer怎么做设计网站可能遇到的问题
  • 怎样建设网站是什么样的网站的软文 怎么做推广
  • 网站需求文档范例怎么做好营销网站开发
  • 长沙网站seo外包网站打不开显示asp
  • 网站建设制度制定贵州网站建设推荐
  • 智能建站系统的建站步骤开发游戏平台
  • 广州网站排名专业乐云seo网站开发有哪些认证
  • 网站推广方式推荐专业做pc 手机网站
  • wordpress网站统计插件网站运营建设岗位职责
  • 一个人做两个博客网站青岛网站建设青岛新思维
  • 那个网站做的调查准确南宁百度seo排名
  • 怎么做国内网站吗深圳头条新闻在线看
  • 网站管理是什么微信小程序免费开店详细步骤
  • 国内高端医疗网站建设梅州生态建设有限公司网站
  • 遵化市城乡建设局网站WordPress自定义登录页面
  • 手机数码网站如何免费创建自己的平台
  • php网站底部文件企业展厅设计要点
  • 国外包装设计欣赏网站长春市长春网站建设网
  • 网站推广策划的策略做网站seo的公司
  • 郑州市城乡建设局网站专门做投标书的网站
  • 程序员 给老婆做网站儿童教育网站源码