stl.string使用总结

本篇总结下STL中string字符串类的使用方法。
1 -- 内部类型定义
typedef traits traits_type;
typedef typename traits::char_type value_type;
typedef size_t size_type;
typedef Allocator allocator_type;
typedef ptrdiff_t difference_type;
typedef Allocator allocator_type;
typedef Allocator allocator_type;
typedef Allocator allocator_type;
2 -- 构造函数
string()

构造空的string,长度为0,不含任何内容
string(const string& str)

拷贝构造函数,使用str将其初始化
string(const string& str, size_type pos, size_type n = npos)

构造string,并使用str从pos开始的n个字符初始化
string(const char* s, size_type n)

构造string,使用字符串s的前n个字符初始化
string(const char *s)

构造string,使用以'\0'结尾的字符串s初始化。
string(int n, char c)

构造string,使用n个字符c初始化
string(InputIterator begin, InputIterator end)

构造string,使用迭代器指定的字符串初始化


除了上述函数,也支持复制运算符构造变量,例如:std::string s = "abcde"。
当构造的string太长而无法表达时,构造函数抛出length_error异常。

3 -- 字符&&字符串操作函数
char operator[] (size_type pos) const

返回pos位置的字符
char & operator[] (size_type pos)

返回pos位置的字符引用
char & at (size_type pos)

返回pos位置的字符引用
const char & at (size_type pos) const

返回pos位置的字符const引用
const char* data () const

返回一个以非NULL终止的字符串
const charT* c_str () const

返回一个以NULL终止的字符串
size_type copy (char* s, size_type n, size_type pos = 0) const

将当前字符串pos位置开始的n个字符拷贝到s字符串中,返回实际拷贝的字符数
4 -- 比较函数
int compare(const string & s) const

将当前string以字典次序与s比较(0:相等;>0:大于s;<0:小于s)
int compare(size_type pos, size_type n, const string& s) const

将当前string从pos开始的n个字符与s比较
int compare(size_type pos, size_type n, const string& s, size_type pos1, size_type n1) const

将当前string的子串(从pos开始的n个字符)与s的子串(从pos1开始的n1个字符)比较
int compare(const char * s) const

将当前string与null-terminated字符串s比较
int compare(size_type pos, size_type n, const char * s, size_type len = npos) const

将当前string的子串(从pos开始的n个字符)与字符串s的前len个字符比较
bool operator == (const string & lhs, const string & rhs)
bool operator == (const char* lhs, const string & rhs)
bool operator == (const string & lhs, const char * rhs)

判断"=="关系是否成立,返回boolean值(全局定义函数,非成员函数)
bool operator != (const string & lhs, const string & rhs)
bool operator != (const char * lhs, const string & rhs)
bool operator != (const string & lhs, const char* rhs)

判断"!="关系是否成立(全局定义函数,非成员函数)
bool operator < (const string & lhs, const string & rhs)
bool operator < (const char* lhs, const string & rhs)
bool operator < (const string & lhs, const char* rhs)

判断"<"关系是否成立(全局定义函数,非成员函数)
bool operator > (const string & lhs, const string & rhs)
bool operator > (const char* lhs, const string & rhs)
bool operator > (const string & lhs, const char * rhs)

判断">"关系是否成立(全局定义函数,非成员函数)
bool operator <= (const string & lhs, const string & rhs)
bool operator <= (const char * lhs, const string & rhs)
bool operator <= (const string & lhs, const char * rhs)

判断"<="关系是否成立(全局定义函数,非成员函数)
bool operator >= (const string & lhs, const string & rhs)
bool operator >= (const char * lhs, const string & rhs)
bool operator >= (const string & lhs, const char * rhs)

判断">="关系是否成立(全局定义函数,非成员函数)
5 -- 查找函数
size_type find(const string& str, size_type pos = 0) const

当前string从pos开始向后查找str第一次出现的位置。
Searches for s as a substring of *this, beginning at character pos of *this.
size_type find(const char* s, size_type pos, size_type n) const

当前string从pos开始向后查找“s子串内前n个字符组成字符串”第一次出现的位置。
Searches for the first n characters of s as a substring of *this, beginning at character pos of *this.
size_type find(const char* s, size_type pos = 0) const

当前string从pos开始向后查找以'\0'结尾子串s第一次出现的位置
Searches for a null-terminated character array as a substring of *this, beginning at character pos of *this.
size_type find(char c, size_type pos = 0) const

当前string从pos开始向后查找字符c第一次出现的位置
Searches for the character c, beginning at character position pos.
size_type rfind(const string & str, size_type pos = npos) const

当前string从pos开始向前查找str第一次出现的位置
Searches backward for s as a substring of *this, beginning at character position min(pos, size())
size_type rfind(const char* s, size_type pos, size_type n) const

当前string从pos开始向前查找“s子串内前n个字符组成字符串”第一次出现的位置
Searches backward for the first n characters of s as a substring of *this, beginning at character position min(pos, size())
size_type rfind(const charT* s, size_type pos = npos) const

当前string从pos开始向前查找以'\0'结尾子串s第一次出现的位置
Searches backward for a null-terminated character array as a substring of *this, beginning at character min(pos, size())
size_type rfind(charT c, size_type pos = npos) const

当前string从pos开始向前查找字符c第一次出现的位置
Searches backward for the character c, beginning at character position min(pos, size()).
size_type find_first_of(const string & s, size_type pos = 0) const

当前string从pos开始向前查找“s中任意一个字符”第一次出现的位置
Searches within *this, beginning at pos, for the first character that is equal to any character within s.
注意:如果pos超过当前string的长度,返回std::string::npos
size_type find_first_of(const char* s, size_type pos, size_type n) const

当前string从pos开始向前查找“s前n个字符中任意一个字符”第一次出现的位置
Searches within *this, beginning at pos, for the first character that is equal to any character within the range [s, s+n).
注意:pos越界直接返回std::string::npos;n越界,会导致越界访问而coredump。
size_type find_first_of(const char* s, size_type pos = 0) const

当前string从pos开始向前查找“以null结尾字符串s中任意一个字符”第一次出现的位置
Equivalent to find_first_of(s, pos, traits::length(s)).
注意:如果pos超过当前string的长度,返回std::string::npos
size_type find_first_of(char c, size_type pos = 0) const

当前string从pos开始向前查找字符c第一次出现的位置
Equivalent to find(c, pos).
size_type find_first_not_of(const string& s, size_type pos = 0) const

当前string从pos开始向前查找第一个不在s中的任意字符位置
Searches within *this, beginning at pos, for the first character that is not equal to any character within s.
size_type find_first_not_of(const char* s, size_type pos, size_type n) const

当前string从pos开始正向(forward)查找第一个不在“s前n个字符”中的任意字符位置
Searches within *this, beginning at pos, for the first character that is not equal to any character within the range [s, s+n).
size_type find_first_not_of(const char* s, size_type pos = 0) const

当前string从pos开始正向(forward)查找不属于“以null结尾字符串s”的第一个字符位置
Searches within *this, beginning at pos, for the first character that is not equal to any character within s.
size_type find_first_not_of(char c, size_type pos = 0) const

当前string从pos开始正向(forward)查找不等于c的第一个字符位置
Searches within *this, beginning at pos, for the first character that is not equal to c.
size_type find_last_of(const string& s, size_type pos = npos) const

当前string从pos开始逆向(backward)查找“s中任意字符”第一次出现的位置
Searches backward within *this, beginning at min(pos, size()), for the first character that is equal to any character within s.
size_type find_last_of(const char * s, size_type pos, size_type n) const

当前string从pos开始逆向(backward)查找“s前n个字符之中任意字符”第一次出现的位置
Searches backward within *this, beginning at min(pos, size()), for the first character that is equal to any character within the first n characters of s.
size_type find_last_of(const char * s, size_type pos = npos) const

当前string从pos开始逆向(backward)查找属于“以null结尾字符串s”的第一个字符位置
Searches backward *this, beginning at min(pos, size()), for the first character that is equal to any character within s.
size_type find_last_of(char c, size_type pos = npos) const

当前string从pos开始逆向(backward)查找字符c第一次出现的位置
Searches backward *this, beginning at min(pos, size()), for the first character that is equal to c.
size_type find_last_not_of(const string& s, size_type pos = npos) const

当前string从pos开始逆向(forward)查找不在s中的任意字符第一次出现的位置
Searches backward within *this, beginning at min(pos, size()), for the first character that is not equal to any character within s.
size_type find_last_not_of(const char* s, size_type pos, size_type n) const

当前string从pos开始逆向(backward)查找不在“s前n个字符”中的任意字符第一次出现的位置
Searches backward within *this, beginning at min(pos, size()), for the first character that is not equal to any character within the first n characters of s.
size_type find_last_not_of(const char* s, size_type pos = npos) const

当前string从pos开始逆向(backward)查找不属于“以null结尾字符串s”的任意字符第一次出现的位置
Searches backward *this, beginning at min(pos, size()), for the first character that is not equal to any character within s.
size_type find_last_not_of(char c, size_type pos = npos) const

当前string从pos开始逆向(backward)查找不等于c的任意字符第一次出现的位置
Searches backward *this, beginning at min(pos, size()), for the first character that is not equal to c.
6 -- 替换函数
std::string & replace(size_type pos1, size_type n1, const std::string & str, size_type pos2 = 0, size_type n2 = npos)

该函数的作用:使用str字符串从位置pos2开始的n2个字符,替换当前字符串从pos1位置开始处的n1个字符。
可以这样理解:该函数将当前字符串从pos1开始的n1个字符全部删除,然后再用str整个字符串或者str从pos2开始的n2个字符,从pos1位置开始填入到当前字符串中。

提醒:如果n1或者n2的数值超出了对应字符串的长度,以实际长度为准,不会出现访问越界的情况。

注意:
a、如果pos1指定的位置超出当前字符串的范围,抛出std::out_of_range异常,不捕捉将导致coredump。
b、如果pos2指定的位置超出替换字符串str的范围,抛出std::out_of_range异常,不捕捉将导致coredump。

std::string& replace(size_type pos, size_type n1, const char * s, size_type n2)

该函数的作用:使用字符串s的前n2个字符,替换当前字符串从pos位置开始处的n1个字符。
可以这样理解:函数将当前字符串从pos开始的n1个字符全部删除,然后再用字符串s的前n2个字符填入到当前字符串中。类似于函数1的pos2等于0,必须指定n2的这种情况,但也有一点的差别,下面会注意里描述这种差别。
注意:
a、如果pos指定的位置超出当前字符串的范围,抛出std::out_of_range异常,不捕捉将导致coredump。
b、该函数不会判断字符串s和n2的大小关系,它严格地从s起始处拷贝n2个字符到指定位置。如果n2表示的长度超出了s的范围,它会读取s后面的内存空间,有可能会因为内存访问越界而coredump。但函数1的n2可以超出范围,它以实际长度为准。
std::string& replace(size_type pos, size_type n1, const char* s)

该函数的作用:使用以'\0'为结尾的字符串s,替换当前字符串从pos位置开始处的n1个字符。
可以这样理解:函数将当前字符串从pos开始的n1个字符全部删除,然后再用字符串s从开始到以'\0'结束的所有字符,从pos位置开始填入到当前字符串中。

注意:如果pos指定的位置超出当前字符串的范围,抛出std::out_of_range异常,不捕捉将导致coredump。

std::string& replace(size_type pos, size_type n1, size_type n2, char c)

该函数的作用:使用n2个c表示的字符,替换当前字符串从pos位置开始处的n1个字符。
可以这么理解:函数将当前字符串从pos开始的n1个字符全部删除,然后再用n2个c字符,从pos位置开始填入到当前字符串中。

注意:如果pos指定的位置超出当前字符串的范围,抛出std::out_of_range异常,不捕捉将导致coredump。

std::string& replace(iterator i1, iterator i2, const std::string& str)

 

该函数的作用:使用字符串str,替换当前字符串[i1,i2)之间的字符。

std::string& replace(iterator i1, iterator i2, const char* s, size_type n)

该函数的作用:使用字符串s的前n个字符,替换当前字符串[i1,i2)之间的字符。

std::string& replace(iterator i1, iterator i2, const char* s)

该函数的作用:使用以'\0'结尾的字符串s,替换当前字符串[i1,i2)之间的字符。

std::string& replace(iterator i1, iterator i2, size_type n, char c)

该函数的作用:使用n个c表示的字符,替换当前字符串[i1,i2)之间的字符。

7 -- 赋值函数
string &operator=(const string &s)

将当前string赋值为s(成员函数)
The assignment operator
string& operator=(const char * s)

将当前string赋值为“以null结尾的字符串s”
Assign a null-terminated character array to a string.
string& operator=(char c)

将当前string赋值为“字符c”
Assign a single character to a string.
string& assign(const string& s)

将当前string赋值为s
Synonym for operator=
string & assign(const string & s, size_type pos, size_type n)

将当前string赋值为“s从pos开始的n个字符”
Assigns a substring of s to *this
string & assign(const char * s, size_type n)

将当前string赋值为“s的前n个字符”
Assigns the first n characters of s to *this.
string& assign(const char* s)

将当前string赋值为“以null结尾的字符串s”
Assigns a null-terminated array of characters to *this.
string & assign(size_type n, char c)

将当前string赋值为“n个字符c”
Erases the existing characters and replaces them by n copies of c.
template <class InputIterator>
basic_string& assign(InputIterator first, InputIterator last)


将当前string赋值为“迭代器[first,last)之间的字符”
Erases the existing characters and replaces them by [first, last)
8 -- 连接函数
string &operator += (const string &s)

将string s连接到当前string尾部
string & operator += (const char * s)

将以null结尾的字符串s连接到当前string的尾部
string & operator += (char c)

将字符c连接到当前string的尾部
string & append(const string & s)

将string s连接到当前string的尾部
string & append(const string & s, size_type pos, size_type n)

将字符串s从pos开始的n个字符连接到当前string的尾部
string & append(const char * s)

将以null结尾的字符串s连接到当前string的尾部
string & append(const char * s, size_type n)

将字符串s的前n个字符连接到当前string的尾部
string & append(size_type n, char c)

将n个字符c连接到当前string的尾部
template <class InputIterator>
string& append(InputIterator first, InputIterator last)


将迭代器[first, last)表示的字符连接到当前string的尾部
9 -- 插入函数
string & insert(size_type pos, const string & s)

在当前string的pos位置之前插入字符串s
string & insert(size_type pos, const string & s, size_type pos1, size_type n)

在当前string的pos位置之前插入“字符串s从pos1位置开始的n个字符”
string & insert(size_type pos, const char* s)

在当前string的pos位置之前插入“以null结尾的字符串s”
string & insert(size_type pos, const char * s, size_type n)

在当前string的pos位置之前插入“字符串s的前n个字符”
string & insert(size_type pos, size_type n, char c)

在当前string的pos位置之前插入“n个字符c”
string::iterator insert(string::iterator pos, char c)

在当前string的pos位置之前插入“一个字符c”
void insert(string::iterator pos, size_type n, char c)

在当前string的pos位置之前插入“n个字符c”
template <class InputIterator >
void insert(string::iterator pos, InputIterator first, InputIterator last)


在当前string的pos位置之前插入“迭代器[first, last)表示的所有字符”
原文地址:https://www.cnblogs.com/motadou/p/1614907.html