C++基础之string类

string也是属于顺序容器,但是string类很重要且经常使用,因此在这里单独记录。

string的操作总结

string(const char *s,int n);   //用c字符串s初始化,s应至少含有n个字符
string(s2,pos2);        //s2从下标pos2开始的字符的拷贝,pos2>s2.size(),构造函数的行为未定义;
string(s2,pos2,len2);      //s2从下标pos2开始的字符的拷贝,pos2>s2.size(),构造函数的行为未定义;不管len2是多少构造函数之多拷贝s2.size()-pos2个字符
构造函数能够接受一个string和const char*参数,而当我们用const char*创建string时,指针指向的数组必须以空字符结尾,拷贝操作遇到空字符时停止;如果还传递给构造函数一个计数值,则不必以空字符结尾。

string类的字符操作:
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异常,下标运算符[]不提供检查访问。
const char *data()const;//返回一个非null终止的c字符数组
const char *c_str()const;//返回一个以null终止的c字符串
size_type copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目


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

注意:size()等函数和下面出现的find返回的是size_type,而不是int类型,常有人把他们划等号,但是他们是不一样的,size_type是string的伙伴类型,是unsigned int类型,虽然,大部分情况用int代替不会出错,但其实有隐患,所以不要把size_type 赋给int。

使用int变量的另一个问题是,有些机器上int变量的表示范围太小,甚至无法存储实际并不长的string对象。如在有16位int型的机器上,int类型变量最大只能表示32767个字符的string对象。而能容纳一个文件内容的string对象轻易就会超过这个数字。因此,为了避免溢出,保存一个string对象size的最安全的方法就是使用标准库类型。

string类的输入输出操作:
string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符' '分开。

 

string的赋值:
string &operator=(const string &s);//把字符串s赋给当前字符串
string &assign(const char *s);//用c类型字符串s赋值,C风格的字符串
string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值,C风格的字符串
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迭代器之间的部分赋给字符串

注意:下面replace()、insert()、find()等函数都包含C风格的字符串的处理方法。

string的连接:
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个字符c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾


string的比较:
bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
size_type compare(const string &s) const;//比较当前字符串和s的大小
size_type compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
size_type 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的子串:
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串

pos和n都有默认值,pos默认为0,n默认为s.size()-pos;如果开始位置超过了string的大小,则substr函数抛出out_of_range的异常。

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


string类的查找函数: 
size_type find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
size_type find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
size_type find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
size_type find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值 
size_type rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
size_type rfind(const char *s, int pos = npos) const;
size_type rfind(const char *s, int pos, int n = npos) const;
size_type rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值 
size_type find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
size_type find_first_of(const char *s, int pos = 0) const;
size_type find_first_of(const char *s, int pos, int n) const;
size_type find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos 
size_type find_first_not_of(char c, int pos = 0) const;
size_type find_first_not_of(const char *s, int pos = 0) const;
size_type find_first_not_of(const char *s, int pos,int n) const;
size_type find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos 
size_type find_last_of(char c, int pos = npos) const;
size_type find_last_of(const char *s, int pos = npos) const;
size_type find_last_of(const char *s, int pos, int n = npos) const;
size_type find_last_of(const string &s,int pos = npos) const; 
size_type find_last_not_of(char c, int pos = npos) const;
size_type find_last_not_of(const char *s, int pos = npos) const;
size_type find_last_not_of(const char *s, int pos, int n) const;
size_type 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::npos = -1;(无符号的)

但是 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别,npos 也就成了该型别的最大无符号值。不过实际数值还是取决于型别 size_type 的实际定义。不幸的是这些最大值都不相同。事实上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是两者型别大小不同)。

string类的替换函数: 
string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s,C风格的字符串
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符,C风格的字符串
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &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个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &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)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串


string类的插入函数: 
string &insert(int p0, const char *s);//C风格的字符串
string &insert(int p0, const char *s, int n);//C风格的字符串
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个字符c
iterator 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


string类的删除函数 
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用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现

 

string的数制转换函数

string to_string (int val);

string to_string (long val);

string to_string (long long val);

string to_string (unsigned val);

string to_string (unsigned long val);

string to_string (unsigned long long val);

string to_string (float val);

string to_string (double val);

string to_string (long double val);

使用C++11引入的C++库函数将string转换为数值类型,相应的库函数申明于头文件<string>中。 

 

名称原型说明
stoi int stoi (const string& str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to integer (function template )
stol long stol (const string& str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to long int (function template)
stoul unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to unsigned integer (function template)
stoll long long stoll (const string& str, size_t* idx = 0, int base = 10);
long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to long long (function template)
stoull unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to unsigned long long (function template)
stof float stof (const string& str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);
Convert string to float (function template )
stod double stod (const string& str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);
Convert string to double (function template )
stold long double stold (const string& str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);
Convert string to long double (function template)

形参说明:
str:重载了string和wstring版本,表示被转换的字符串。

idx:表示一个size_t*的指针类型,默认为空值。不为空时,转换成功时获取第一个非数值字符的下标。一般情况下,因为它是直接char型指针把最后非数值字符的地址值和起始地址值相减,所以也表示成功转换的字符数量,如”10”转成功为数值10时,*idx的值为2。

base:表示转换基准,默认是10进制。

注意:如果string不能转换成为一个数值,这些函数会抛出invalid_argument异常,如果转换的数值不能用任何类型来表示,则抛出out_of_range异常。

原文地址:https://www.cnblogs.com/yeqluofwupheng/p/6711125.html