收藏 分销(赏)

初始化string对象的方式总结.doc

上传人:a199****6536 文档编号:1822560 上传时间:2024-05-09 格式:DOC 页数:19 大小:37.04KB
下载 相关 举报
初始化string对象的方式总结.doc_第1页
第1页 / 共19页
初始化string对象的方式总结.doc_第2页
第2页 / 共19页
初始化string对象的方式总结.doc_第3页
第3页 / 共19页
初始化string对象的方式总结.doc_第4页
第4页 / 共19页
初始化string对象的方式总结.doc_第5页
第5页 / 共19页
点击查看更多>>
资源描述

1、.标准c+中string类函数介绍注意不是CString之所以抛弃char*的字符串而选用C+标准程序库中的string类,是因为他和前者比拟起来,不必 担忧内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 = 进行赋值操作,= 进行比拟,+ 做串联是不是很简单?。我们尽可以把它看成是C+的根本数据类型。好了,进入正题首先,为了在我们的程序中使用string类型,我们必须包含头文件 。如下:#include /注意这里不是string.h string.h是C字符串头文件#include using namespac

2、e 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) /将

3、字符串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+字符串的重点,我先把各种操作函数罗列出来,不喜欢把所有函数都看完的

4、人可以在这里找自己喜欢的函数,再到后面看他的详细解释。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() /返回重新

5、分配之前的字符容量n) reserve() /保存一定量内存以容纳一定数量的字符o) , at() /存取单一字符p) ,getline() /从stream读取某值q) ,=,=,=,!=,甚至支持string与C-string的比拟(如 str,=,=这些操作符的时候是根据“当前字符特性将字符按字典顺序进行逐一得 比拟。字典排序靠前的字符小,比拟的顺序是从前向后比拟,遇到不相等的字符就按这个位置上的两个字符的比拟结果确定两个字符串的大小。同时,string (“aaaa) string(aaaaa)。另一个功能强大的比拟函数是成员函数compare()。他支持多参数处理,支持用索引值和长度

6、定位子串来进行比拟。他返回一个整数来表示比拟结果,返回值意义如下:0-相等 0-大于 从输入流读取一个string。2用于输入,同样重载运算符operator,=,时返回1,时返回-1,=时返回0 string的子串: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(

7、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(c

8、onst 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

9、, 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:npos int find_first_not_of(char c, int pos = 0) const;int find_first_not_of(const char *s, int pos = 0) const;int find_fi

10、rst_not_of(const char *s, int pos,int n) const;int find_first_not_of(const string &s,int pos = 0) const;/从当前串中查找第一个不在串s中的字符出现的位置,失败返回string:npos int 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)

11、 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_

12、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个字符,然后在

13、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

14、, 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

15、_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个字符stri

16、ng &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, iterat

17、or 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不允许改变迭代

18、的内容。常用迭代器函数有: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用于从后向前的迭代访问,通过设置迭代器string:reverse_iterator,string:const_reverse_iterator实现字符串流处理: 通过定义ostringstream和istringstream变量实现,头文件中例如:string input(hello,this is a test);istringstream is(input);string s1,s2,s3,s4;iss1s2s3s4;/s1=hello,this,s2=is,s3=a,s4=testostringstream os;oss1s2s3s4;coutos.str();实用文档.

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服