郑州网站定制,百度官网首页,网站关键词下降,住房与建设管理局文章目录 一string类1C语言中的字符串2C中的string二遍历1[ ]2迭代器3const迭代器4范围for5auto6总结三String的尾插1size和length2max_size,capacity和clear3访问接口4尾插字符和字符串5 append的重载三string的扩容问题#xff08;1#xff09;怎么扩容#xff08;2#… 文章目录 一string类1C语言中的字符串2C中的string二遍历1[ ]2迭代器3const迭代器4范围for5auto6总结三String的尾插1size和length2max_size,capacity和clear3访问接口4尾插字符和字符串5 append的重载三string的扩容问题1怎么扩容2reserve和reverse 一string类
1C语言中的字符串 C语言中字符串是以’\0’结尾的一些字符的集合为了操作方便C标准库中提供了一些str系列 的库函数但是这些库函数与字符串是分离开的不太符合OOP的思想而且底层空间需要用户 自己管理稍不留神可能还会越界访问。 姓名。地址 2C中的string 在使用string类时必须包含#include头文件以及using namespace std; auto和范围for 我们马上在下文会提到这两个特别好用的用法希望大家注意 string类本身是basic_string typedef的过来的 basic_string本身是一个模板 注意事项 (1)c中的string类是包含在string头文件里的所以要包含string (2)容器和类里面一般会有接口包括一些写好的函数和特殊定义比如size,capacity都叫接口 (3)c一开始设计的时候没有容器的概念string与后面的容器有一些不一样。具体表现是string的 接口非常多而杂有些甚至重复定义这是为了与后面的迭代器保持一致但是以前多的不能删后面c的内容也只能兼容string. 二遍历
1[ ] 这里[ ]属于element access(元素获取)个人认为有点像访问的关键字 这里设置的非常巧妙本来string类不能像数组一样访问下标直接访问或者遍历全部元素的但是我们可以认为规定位置然后返回进行访问这样起到了数组下标访问问的效果 这样我们就可以进行遍历 2迭代器 迭代器差不多类似于指针但是底层不同string的迭代器类似于指针下面it1类似于指针cout*it1类似于解引用。 但其他的容器不一定。 迭代器差不多类似于指针但是底层不同string的迭代器类似于指针但其他的容器不一定。 反向迭代器从end往begin输出 3const迭代器 const对象不能使用普通迭代器 看上面的const对象如果像进行定义就要进行const修饰迭代器这也是权限的放大 。 4范围for 我们以前遍历一些东西都要用for int ixx;ixxx;i但是我们学了范围for可以方便的定义遍历 for(xx a:xxx) 是吧xxx从头到位赋值给a然后一个个输出编译器自动识别开头和结尾 、 xx是定义的类型 5auto 四种迭代器iterate,const iterate,reverse_ iterateconst reverse_ iterate 然后可以定义后直接 auto xxxxxx.begin();这样不用指明很长的类型 然后上面的for遍历for(xx a:xxx) xx可以写成auto,自动识别xxx的类型给auto 但是不会推出引用要自己写一个 现在auto引用时定义迭代器可以短一点 修改的时候要加因为是把s1一个个赋值给ch然后输出。下次cout时候就没用了 但是一开始赋值给别名就进行前置修改了。 6总结 三种遍历方法[],迭代器范围for 范围for底层是迭代器所以只有容器中支持迭代器的才支持范围for 因为数组底层和迭代器差不多所以也支持范围for auto是一个自动识别的类型在定义迭代器类型一般是很多的字母可以用auto 三String的尾插
1size和length 推荐size 因为其他容器也兼容size,这就二中注意事项中string臃肿的部分 2max_size,capacity和clear 这三个都是接口,max_size是最大下标 capacity和栈中一样是空间可以扩容 clear是把size直接搞成0。 3访问接口
at,[] 我们在遍历中知道[ ]怎么访问的了 下面我们介绍at at和[]的不同是如果访问到非法处理方式不一样at要抛出异常然后捕获what()如图 []异常是 4尾插字符和字符串 尾插字符是push_back 尾插字符串是append 5 append的重载 我们不仅可以用接口接口其实也有许多重载这里我们就不细说了 三string的扩容问题
1怎么扩容 根据编译器的不同扩容方式不一样这里是vs2022它在后面一般是1.5倍的扩容。 2reserve和reverse reverse是一个扩容接口函数反正我们如果提前知道要扩容多大可以直接提前reverse他会提前开好一个比较大的空间这样就不要频繁的扩容在堆上开空间 reverse有反转的意思一般命名时候用到上文的反向迭代器就加了reverse的前缀。
文章转载自: http://www.morning.hqgxz.cn.gov.cn.hqgxz.cn http://www.morning.tjcgl.cn.gov.cn.tjcgl.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.srbbh.cn.gov.cn.srbbh.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.srgwr.cn.gov.cn.srgwr.cn http://www.morning.gbcnz.cn.gov.cn.gbcnz.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.dqpnd.cn.gov.cn.dqpnd.cn http://www.morning.gmyhq.cn.gov.cn.gmyhq.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.paxkhqq.cn.gov.cn.paxkhqq.cn http://www.morning.srbfp.cn.gov.cn.srbfp.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.pkdng.cn.gov.cn.pkdng.cn http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.trzmb.cn.gov.cn.trzmb.cn http://www.morning.bpmdg.cn.gov.cn.bpmdg.cn http://www.morning.xhfky.cn.gov.cn.xhfky.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.knlgk.cn.gov.cn.knlgk.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.lqypx.cn.gov.cn.lqypx.cn http://www.morning.rbnnq.cn.gov.cn.rbnnq.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.feites.com.gov.cn.feites.com http://www.morning.xhqr.cn.gov.cn.xhqr.cn http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn http://www.morning.pszw.cn.gov.cn.pszw.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.blxor.com.gov.cn.blxor.com http://www.morning.dfndz.cn.gov.cn.dfndz.cn http://www.morning.mfjfh.cn.gov.cn.mfjfh.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.ytbr.cn.gov.cn.ytbr.cn http://www.morning.hnrqn.cn.gov.cn.hnrqn.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.fxzlg.cn.gov.cn.fxzlg.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.mbnhr.cn.gov.cn.mbnhr.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.zwgrf.cn.gov.cn.zwgrf.cn http://www.morning.xjqkh.cn.gov.cn.xjqkh.cn http://www.morning.nbgfk.cn.gov.cn.nbgfk.cn http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.rflcy.cn.gov.cn.rflcy.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.nlgnk.cn.gov.cn.nlgnk.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.qhnmj.cn.gov.cn.qhnmj.cn http://www.morning.dblfl.cn.gov.cn.dblfl.cn