100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > STL之字符串类模板 string(三) C++ string类成员函数

STL之字符串类模板 string(三) C++ string类成员函数

时间:2023-12-07 18:10:29

相关推荐

STL之字符串类模板 string(三) C++ string类成员函数

前言

之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 = 进行赋值操作,== 进行比较,+ 做串联。我们尽可以把它看成是C++的基本数据类型。

构造函数

功能

产生或复制字符串构造

string(const char *s); //用字符串s初始化string(int n,char c);//用n个字符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的初值

析构函数

功能

销毁字符串方式

s.~string() //销毁所有字符,释放内存

字符串操作函数

这里是C++字符串的重点,这里我们先罗列,后面在分别做详细解释,这样的话可能会显得稍微清晰一点。

=,assign() //赋以新值swap() //交换两个字符串的内容+=,append(),push_back() //在尾部添加字符insert() //插入字符erase() //删除字符clear() //删除全部字符replace() //替换字符+ //串联字符串==,!=,<,<=,>,>=,compare() //比较字符串size(),length() //返回字符数量max_size() //返回字符的可能最大个数empty() //判断字符串是否为空capacity() //返回重新分配之前的字符容量reserve() //保留一定量内存以容纳一定数量的字符[ ], at() //存取单一字符>,getline() //从stream读取某值<< //将谋值写入streamcopy() //将某值赋值为一个C_stringc_str() //将内容以C_string返回data() //将内容以字符数组形式返回substr() //返回某个子字符串find() //查找函数begin() end() //提供类似STL的迭代器支持rbegin() rend() //逆向迭代器get_allocator() //返回配置器

=,assign()

功能

C++ string类的成员函数,用于拷贝、赋值操作,它们允许我们顺序地把一个string 对象的部分内容拷贝到另一个string 对象上。说明

string &operator=(const string &s);//把字符串s赋给当前字符串string &assign(const char *s);//用c类型字符串s赋值string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值string &assign(const string &s);//把字符串s赋给当前字符串string &assign(int n,char c);//用n个字符c赋值给当前字符串string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串

使用方式 直接用另一个字符串赋值,如:

str2.assign(str1);//即用str1给str2赋值.

用另一个字符串的一个子串赋值,如:

str3.assign(str1, 2, 3);

用一个字符串的前一段子串赋值,如:

str4.assign(“World”, 5);

用几个相同的字符,赋值,如:

str5.assign(10, ‘c’);

/*************************************************// Copyright (C), -221, Sunking&Yang. Co., Ltd.// 文件名: xxx.cpp// 作者: SunkingYang// 版本: 1.0// 日期: /04/09//博客地址:/y601500359// 说明:原创不易,转载请注明来源!*************************************************/#include<iostream>#include <string>using namespace std;//========================================int main(){std::string str1("hello");string str2;string str3;string str4;string str5;//====================================str2.assign(str1);str3.assign("World", 4);str4.assign(str1, 2, 3);str5.assign(10, 'c');//====================================cout << str1 << endl;cout << str2 << endl;cout << str3 << endl;cout << str4 << endl;cout << str5 << endl;//====================================system("pause");return 0;}

运行效果图:

Swap

void swap(string &s2); //交换当前字符串与s2的值

+=,append( ),push_back()

功能:

在尾部添加字符说明:

string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾string &append(const char *s); //把c类型字符串s连接到当前字符串结尾string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾string &append(const string &s); //同operator+=()string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾string &append(int n,char c); //在当前字符串结尾添加n个字符cstring &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾

使用例子:

s+=str;//加个字符串s+=”my name is jiayp”;//加个C字符串s+=’a’;//加个字符s.append(str);s.append(str,1,3);s.append(str,2,string::npos)s.append(“my name is jiayp”);s.append(“nico”,5);s.append(5,’x’);s.push_back(‘a’);//这个函数只能增加单个字符对STL熟悉的理解起来很简单

insert()

功能

插入字符说明

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个字符c

erase()

功能:删除字符

iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串

clear()

功能:删除全部字符

string str;str="balabala";s.clear();

replace()

功能:替换字符

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 str1 = "abc";string str2 = "def";string str3 = str1 + str2;

==,!=,<,<=,>,>=,compare()

功能:比较字符串

int compare(const string &s) const;//比较当前字符串和s的大小int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始n个字符字符串与s中pos2开始n2个字符字符串的大小int compare(const char *s) const;int compare(int pos, int n,const char *s) const;int compare(int pos, int n,const char *s, int pos2) const;

compare函数在>时返回1,<时返回-1,==时返回0

string的特性描述

size(),length()

功能:返回字符数量max_size()

功能:返回字符的可能最大个数empty()

功能:判断字符串是否为空capacity()

功能:返回重新分配之前的字符容量

int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)int max_size()const; //返回string对象中可存放的最大字符串的长度int size()const; //返回当前字符串的大小int length()const; //返回当前字符串的长度bool empty()const; //当前字符串是否为空void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分

reserve()

功能:保留一定量内存以容纳一定数量的字符

[ ], at()

功能:存取单一字符

const char &operator[](int n)const;const char &at(int n)const;char &operator[](int n);char &at(int n);

operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。

输入输出操作

>>,getline()

功能:从stream读取某值<<

功能:将谋值写入stream

string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作

getline(istream &in,string &s); //用于从输入流in中读取字符串到s中,以换行符'\n'分开//eg:string s1;getline (cin, s1); // 实现了读取一行字符,包括空格、制表符、回车符等行内字符和符号,以\n分开

copy()

功能:将某值赋值为一个C_stringc_str()

功能:将内容以C_string返回data()

功能:将内容以字符数组形式返回

const char *data()const;//返回一个非null终止的c字符数组const char *c_str()const;//返回一个以null终止的c字符串int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目

substr()

功能:返回某个子字符串

string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串

find()

功能:查找函数

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相似,只不过是从后向前查找

迭代器处理 const_iterator

begin() end()

功能:提供类似STL的迭代器支持rbegin() rend()

功能:逆向迭代器

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实现

get_allocator()

功能:返回配置器

参考链接1:/view/1441.html

参考链接2:/sinat_36184075/article/details/54836053

参考链接3:/Draymonder/p/6944479.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。