电影院做羞羞的网站,seo官网,自适应网站建设都找全网天下,wordpress 做wiki目录 前言 1. string使用 2. string的常见构造 3. string类对象的访问及遍历 迭代器遍历#xff1a; 访问#xff1a; 4. string类对象的容量操作
4.1 size和length 4.2 clear、empty和capacity 4.3 reserve和resize
reserve resize 5. string类对象的修改操作 push_back o…目录 前言 1. string使用 2. string的常见构造 3. string类对象的访问及遍历 迭代器遍历 访问 4. string类对象的容量操作
4.1 size和length 4.2 clear、empty和capacity 4.3 reserve和resize
reserve resize 5. string类对象的修改操作 push_back operator c_str find
substr 6. insert和erase insert
erase
总结 前言 STL(standard template libaray-标准模板库)是C标准库的重要组成部分不仅是一个可复用的组件库而且是一个包罗数据结构与算法的软件框架主要包含算法、仿函数、迭代器、空间适配器、容器、适配器六大组件本无主要介绍的是STL容器中string常用函数的用法。 1. string使用 string是表示字符串的字符串类它的接口与常规容器的接口基本相同并且添加了一些专门用来操作string的常用接口在刷题时也经常使用。 使用string类时必须包含#includestring头文件以及using namespace std; 2. string的常见构造 C的标准库版本不同string构造方法也会有所不同 详细可查阅https://legacy.cplusplus.com/reference/string/string/string/ 无参构造
string s1; 字符构造
//string str(x); //这样不行
string s;
s x; // 也就是赋值运算符重载 字符串常量构造
string s2(hello world!);
string s2 hello world!;拷贝构造
string s3 s2; // 两种方法等价
string s4(s2); 指定n个相同字符进行初始化
// 接口原型string (size_t n, char c)
string s6(10, x);//10个x初始化 迭代器区间构造
string s7 Hello World!;
// 创建一个迭代器区间从第2个字符开始到第11个字符结束
string s8(s7.begin() 1, s7.begin() 11); // ello World 3. string类对象的访问及遍历 迭代器遍历 begin和end
begin返回指向字符串的第一个字符的迭代器end返回指向字符串末尾的迭代器
string s1 Hello world!;
//迭代器
// iterator用法像指针
string::iterator it s1.begin();
while (it ! s1.end())
{cout *it ;it;
} 反向遍历rbegin和rend rbegin返回指向字符串最后一个字符即其反向开头的反向迭代器 rend 返回一个反向迭代器该迭代器指向字符串第一个字符被视为其反向末尾
auto it1 str.rbegin();
while (it1 ! str.rend())
{cout *it1 ;it1;
} 范围for遍历
for (auto e : s1)
{cout e;
} 访问 [ ]下标访问
s1[1] x;
cout s1[1]; 4. string类对象的容量操作 string类对象支持直接获取字符串长度length和size都可以返回有效字符长度size更通用
4.1 size和length
string s Hello world!;// 12个字符
cout s.size() endl; // 12
cout s.length() endl;// 12 4.2 clear、empty和capacity 注意
clear后,使用empty会返回trueclear函数只将size清零clear函数不会改变capacity 使用非常简单便捷
string s Hello world!;//VS环境下,扩容为1.5倍扩容环境不同capacity结果可能不同
cout s.capacity() endl; // 15
cout s.empty() endl; // 0 -false
s.clear();
cout s.empty() endl; // 1 -true
cout s.capacity() endl; // 15 4.3 reserve和resize reserve和resize有些相似都具有扩容的功能。
reserve
reserve的主要功能就是开空间为string对象预留空间提前开好空间减少扩容提高效率 环境不同开空间规则也不同
在VS环境下为1.5倍扩容初始容量是15 g环境下从0开始二倍扩容
string s1 ;
cout s1.capacity() endl; // 15
s1.reserve(30);
cout s1.capacity() endl; // 31
reserve在一般的编译器中不具备缩容的功能C没有严格的去规定 resize resize功能是改变字符串的size
string s Hello world!;
cout s endl; // 输出Hello world!
cout s.size() endl; // 12
cout s.capacity() endl; // 15s.resize(40);
cout s endl; // 输出Hello world!
cout s.size() endl; // 40
cout s.capacity() endl; // 47 在没有给字符时resize默认补的是\0.
string s Hello world!;
s.resize(15,x); // 输出Hello world!xxx
这个功能也可以让它用来初始化string对象。
resize值小于字符串的size会删除字符串中的有效字符
string s Hello world!;
cout s endl;
cout s.size() endl; // 12
cout s.capacity() endl; // 15s.resize(5);
cout s endl; //输出Hello
cout s.size() endl; // 5
cout s.capacity() endl; // 15 5. string类对象的修改操作 push_back
string s(abcde);
s.push_back(f);
s.push_back(g); operator
append使用的频率不高主要使用
string str1 Hello;
string str2 World;
str1 str2; // 现在 str1 的值为 HelloWorldc_str
string str Hello;
const char* cstr str.c_str(); // cstr 指向包含 Hello 的C风格字符串
c_str() 是C中string 类的成员函数它返回一个指向以空字符结尾的C风格字符串的指针以便与需要C风格字符串作为参数的函数进行交互。
注意 返回的指针指向的字符串是只读的不能用于修改 find
find用于在字符串中查找子字符串的位置如果找不到则返回 string::npos string str Hello, World!;
size_t pos str.find(World); // pos 的值为 7substr
substr用于从字符串中提取子字符串
//函数原型
string substr (size_t pos 0, size_t len npos) const;pos 是要提取的子字符串的起始位置len 是要提取的子字符串的长度默认值为 npos表示提取从起始位置到字符串末尾的所有字符
string str Hello, World!;
string sub str.substr(7, 5); // sub 的值为 World6. insert和erase insert
insert的函数原型种类很多但最长用的也就是在指定位置插入单个字符、字符串、 一个string类对象等感兴趣可以去了解一下
string str Hello!;
str.insert(5, World); // 现在 str 的值为 Hello World!当然它也支持使用迭代器插入数据
erase erase 函数用于从指定位置开始删除指定长度的字符函数原型
string erase (size_t pos 0, size_t len npos);它也支持使用迭代器区间删除数据
示例
string str Hello, World!;
str.erase(7, 7); // 现在 str 的值为 Hello, 总结 本文主要是string类常用接口的总结string发布时间早于STL在最初设计时实现的功能丰富接口繁多也较为复杂STL库函数中很多接口都十分相似,学习了string后会对vector和list等等容器的学习有帮助以上便是本文全部内容希望对你有所帮助感谢阅读 文章转载自: http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.lzqtn.cn.gov.cn.lzqtn.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.bctr.cn.gov.cn.bctr.cn http://www.morning.gywfp.cn.gov.cn.gywfp.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.gmyhq.cn.gov.cn.gmyhq.cn http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.csnch.cn.gov.cn.csnch.cn http://www.morning.mhrzd.cn.gov.cn.mhrzd.cn http://www.morning.lmqfq.cn.gov.cn.lmqfq.cn http://www.morning.lndongguan.com.gov.cn.lndongguan.com http://www.morning.wqkzf.cn.gov.cn.wqkzf.cn http://www.morning.tgnwt.cn.gov.cn.tgnwt.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.ytbr.cn.gov.cn.ytbr.cn http://www.morning.sqmlw.cn.gov.cn.sqmlw.cn http://www.morning.qblcm.cn.gov.cn.qblcm.cn http://www.morning.mtbth.cn.gov.cn.mtbth.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.rnpt.cn.gov.cn.rnpt.cn http://www.morning.crkhd.cn.gov.cn.crkhd.cn http://www.morning.ryrpq.cn.gov.cn.ryrpq.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.qphgp.cn.gov.cn.qphgp.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.mpxbl.cn.gov.cn.mpxbl.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.ghzfx.cn.gov.cn.ghzfx.cn http://www.morning.mjbnp.cn.gov.cn.mjbnp.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.rnmyw.cn.gov.cn.rnmyw.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.gdpai.com.cn.gov.cn.gdpai.com.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.kgxrq.cn.gov.cn.kgxrq.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.grpfj.cn.gov.cn.grpfj.cn http://www.morning.wgqtj.cn.gov.cn.wgqtj.cn http://www.morning.nydgg.cn.gov.cn.nydgg.cn http://www.morning.wdpt.cn.gov.cn.wdpt.cn http://www.morning.kysport1102.cn.gov.cn.kysport1102.cn http://www.morning.kpwcx.cn.gov.cn.kpwcx.cn http://www.morning.fdsbs.cn.gov.cn.fdsbs.cn http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn http://www.morning.jwbfj.cn.gov.cn.jwbfj.cn http://www.morning.cryb.cn.gov.cn.cryb.cn http://www.morning.bcngs.cn.gov.cn.bcngs.cn http://www.morning.tftw.cn.gov.cn.tftw.cn http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.mqldj.cn.gov.cn.mqldj.cn http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.xqtqm.cn.gov.cn.xqtqm.cn http://www.morning.ppqzb.cn.gov.cn.ppqzb.cn http://www.morning.rxzcl.cn.gov.cn.rxzcl.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.yrflh.cn.gov.cn.yrflh.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.ypxyl.cn.gov.cn.ypxyl.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.skkln.cn.gov.cn.skkln.cn