




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
标准c+中string类函数介绍注意不是CString之所以抛弃char*的字符串而选用C+标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 = 进行赋值操作,= 进行比较,+ 做串联(是不是很简单?)。我们尽可以把它看成是C+的基本数据类型。好了,进入正题首先,为了在我们的程序中使用string类型,我们必须包含头文件 。如下:#include /注意这里不是string.h string.h是C字符串头文件#include using namespace std;1声明一个C+字符串声明一个字符串变量很简单:string Str;这样我们就声明了一个字符串变量,但既然是一个类,就有构造函数和析构函数。上面的声明没有传入参数,所以就直接使用了string的默认的构造函数,这个函数所作的就是把Str初始化为一个空字符串。String类的构造函数和析构函数如下:a) string s; /生成一个空字符串sb) string s(str) /拷贝构造函数 生成str的复制品c) string s(str,stridx) /将字符串str内“始于位置stridx”的部分当作字符串的初值d) string s(str,stridx,strlen) /将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值e) string s(cstr) /将C字符串作为s的初值f) string s(chars,chars_len) /将C字符串前chars_len个字符作为字符串s的初值。g) string s(num,c) /生成一个字符串,包含num个c字符h) string s(beg,end) /以区间beg;end(不包含end)内的字符作为字符串s的初值i) s.string() /销毁所有字符,释放内存都很简单,我就不解释了。2字符串操作函数这里是C+字符串的重点,我先把各种操作函数罗列出来,不喜欢把所有函数都看完的人可以在这里找自己喜欢的函数,再到后面看他的详细解释。a) =,assign() /赋以新值b) swap() /交换两个字符串的内容c) +=,append(),push_back() /在尾部添加字符d) insert() /插入字符e) erase() /删除字符f) clear() /删除全部字符g) replace() /替换字符h) + /串联字符串i) =,!=,=,compare() /比较字符串j) size(),length() /返回字符数量k) max_size() /返回字符的可能最大个数l) empty() /判断字符串是否为空m) capacity() /返回重新分配之前的字符容量n) reserve() /保留一定量内存以容纳一定数量的字符o) , at() /存取单一字符p) ,getline() /从stream读取某值q) ,=,=,=,!=),甚至支持string与C-string的比较(如 str,=,=这些操作符的时候是根据“当前字符特性”将字符按字典顺序进行逐一得 比较。字典排序靠前的字符小,比较的顺序是从前向后比较,遇到不相等的字符就按这个位置上的两个字符的比较结果确定两个字符串的大小。同时,string (“aaaa”) string(aaaaa)。另一个功能强大的比较函数是成员函数compare()。他支持多参数处理,支持用索引值和长度定位子串来进行比较。他返回一个整数来表示比较结果,返回值意义如下:0-相等 0-大于 从输入流读取一个string。2用于输入,同样重载运算符operator,=,时返回1,时返回-1,=时返回0string的子串:string substr(int pos = 0,int n = npos) const;/返回pos开始的n个字符组成的字符串string的交换:void swap(string &s2); /交换当前字符串与s2的值string类的查找函数:int find(char c, int pos = 0) const;/从pos开始查找字符c在当前字符串的位置int find(const char *s, int pos = 0) const;/从pos开始查找字符串s在当前串中的位置int find(const char *s, int pos, int n) const;/从pos开始查找字符串s中前n个字符在当前串中的位置int find(const string &s, int pos = 0) const;/从pos开始查找字符串s在当前串中的位置/查找成功时返回所在位置,失败返回string:npos的值int rfind(char c, int pos = npos) const;/从pos开始从后向前查找字符c在当前串中的位置int rfind(const char *s, int pos = npos) const;int rfind(const char *s, int pos, int n = npos) const;int rfind(const string &s,int pos = npos) const;/从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string:npos的值int find_first_of(char c, int pos = 0) const;/从pos开始查找字符c第一次出现的位置int find_first_of(const char *s, int pos = 0) const;int find_first_of(const char *s, int pos, int n) const;int find_first_of(const string &s,int pos = 0) const;/从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string:nposint find_first_not_of(char c, int pos = 0) const;int find_first_not_of(const char *s, int pos = 0) const;int find_first_not_of(const char *s, int pos,int n) const;int find_first_not_of(const string &s,int pos = 0) const;/从当前串中查找第一个不在串s中的字符出现的位置,失败返回string:nposint find_last_of(char c, int pos = npos) const;int find_last_of(const char *s, int pos = npos) const;int find_last_of(const char *s, int pos, int n = npos) const;int find_last_of(const string &s,int pos = npos) const;int find_last_not_of(char c, int pos = npos) const;int find_last_not_of(const char *s, int pos = npos) const;int find_last_not_of(const char *s, int pos, int n) const;int find_last_not_of(const string &s,int pos = npos) const;/find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找string类的替换函数:string &replace(int p0, int n0,const char *s);/删除从p0开始的n0个字符,然后在p0处插入串sstring &replace(int p0, int n0,const char *s, int n);/删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符string &replace(int p0, int n0,const string &s);/删除从p0开始的n0个字符,然后在p0处插入串sstring &replace(int p0, int n0,const string &s, int pos, int n);/删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符string &replace(int p0, int n0,int n, char c);/删除p0开始的n0个字符,然后在p0处插入n个字符cstring &replace(iterator first0, iterator last0,const char *s);/把first0,last0)之间的部分替换为字符串sstring &replace(iterator first0, iterator last0,const char *s, int n);/把first0,last0)之间的部分替换为s的前n个字符string &replace(iterator first0, iterator last0,const string &s);/把first0,last0)之间的部分替换为串sstring &replace(iterator first0, iterator last0,int n, char c);/把first0,last0)之间的部分替换为n个字符cstring &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);/把first0,last0)之间的部分替换成first,last)之间的字符串string类的插入函数:string &insert(int p0, const char *s);string &insert(int p0, const char *s, int n);string &insert(int p0,const string &s);string &insert(int p0,const string &s, int pos, int n);/前4个函数在p0位置插入字符串s中pos开始的前n个字符string &insert(int p0, int n, char c);/此函数在p0处插入n个字符citerator insert(iterator it, char c);/在it处插入字符c,返回插入后迭代器的位置void insert(iterator it, const_iterator first, const_iterator last);/在it处插入first,last)之间的字符void insert(iterator it, int n, char c);/在it处插入n个字符cstring类的删除函数iterator erase(iterator first, iterator last);/删除first,last)之间的所有字符,返回删除后迭代器的位置iterator erase(iterator it);/删除it指向的字符,返回删除后迭代器的位置string &erase(int pos = 0, int n = npos);/删除pos开始的n个字符,返回修改后的字符串string类的迭代器处理:string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。用string:iterator或string:const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:const_iterator begin()const;iterator begin(); /返回string的起始位置const_iterator end()const;iterator end(); /返回string的最后一个字符后面的位置const_iterator rbegin()const;iterator rbegin(); /返回string的最后一个字符的位置const_iterator rend()const;iterator rend(); /返回string第一个字符位置的前面rbegin和rend用于从后
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 济宁市专业技术人员继续教育公需科目网上考试试题及答案
- 2025年新能源汽车零部件再制造技术路线与发展趋势报告
- 银行岗位考前冲刺练习试题一套附答案详解
- 2025年网络直播规范化与商业模式创新:短视频平台与直播平台融合报告
- 2025年康复医疗器械市场调研报告:需求分析及产品创新策略探讨
- 2025至2030年中国龙眼干行业市场全景调研及投资规划建议报告
- 基础强化四川省峨眉山市7年级上册期中测试卷章节测试试题(含答案解析)
- 2025至2030年中国手工纸制造行业市场发展现状及投资方向研究报告
- 押题宝典高校教师资格证之《高等教育心理学》通关考试题库及答案详解(名师系列)
- 解析卷-人教版(五四制)6年级数学下册期末试题带答案详解(黄金题型)
- 隧道二衬安全注意事项
- 连锁餐饮学生管理办法
- 绿色矿山培训课件
- 银行科技架构管理办法
- 小学冠词教学课件
- 110接处警课件培训
- 大模型时代:生成式AI发展与科技创新范式
- 黄浦区2024-2025学年六年级下学期期末考试数学试卷及答案(上海新教材沪教版)
- 2025-2030中国香皂市场销售动态及竞争策略分析报告
- 劳动仲裁内部培训
- 中国方言课件图文教学
评论
0/150
提交评论