要想使用标准C++中string类型,必须要包含
1 | #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 |
或
下面你就可以使用string/wstring了,它们两分别对应着char和wchar_t。
string和wstring的用法是一样的,以下只用string作介绍:
string类的构造函数:
此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2=”hello”;都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常 ;
string类的字符操作:
1 | const char &operator[]( int n) const ; |
2 | const char &at( int n) const ; |
3 | char &operator[]( int n); |
operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
1 | const char *data() const ; |
2 | const char *c_str() const ; |
3 | int copy( char *s, int n, int pos = 0) const ; |
string的特性描述:
6 | void resize( int len, char c); |
string类的输入输出操作:
string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符’\n’分开。
string的赋值:
1 | string &operator=( const string &s); |
2 | string &assign( const char *s); |
3 | string &assign( const char *s, int n); |
4 | string &assign( const string &s); |
5 | string &assign( int n, char c); |
6 | string &assign( const string &s, int start, int n); |
7 | string &assign(const_iterator first,const_itertor last); |
string的连接:
1 | string &operator+=( const string &s); |
2 | string &append( const char *s); |
3 | string &append( const char *s, int n); |
4 | string &append( const string &s); |
5 | string &append( const string &s, int pos, int n); |
6 | string &append( int n, char c); |
7 | string &append(const_iterator first,const_iterator last); |
string的比较:
1 | bool operator==( const string &s1, const string &s2) const ; |
运算符”>”,”<”,”>=”,”<=”,”!=”均被重载用于字符串的比较;
1 | int compare( const string &s) const ; |
2 | int compare( int pos, int n, const string &s) const ; |
3 | int compare( int pos, int n, const string &s, int pos2, int n2) const ; |
//pos2开始的n2个字符组成的字符串的大小
1 | int compare( const char *s) const ; |
2 | int compare( int pos, int n, const char *s) const ; |
3 | int compare( int pos, int n, const char *s, int pos2) const ; |
compare函数在>时返回1,<时返回-1,==时返回0
string的子串:
1 | string substr( int pos = 0, int n = npos) const ; |
string的交换:
string类的查找函数:
1 | int find( char c, int pos = 0) const ; |
2 | int find( const char *s, int pos = 0) const ; |
3 | int find( const char *s, int pos, int n) const ; |
4 | int find( const string &s, int pos = 0) const ; |
//查找成功时返回所在位置,失败返回string::npos的值
1 | int rfind( char c, int pos = npos) const ; |
2 | int rfind( const char *s, int pos = npos) const ; |
3 | int rfind( const char *s, int pos, int n = npos) const ; |
4 | int rfind( const string &s, int pos = npos) const ; |
6 | int find_first_of( char c, int pos = 0) const ; |
7 | int find_first_of( const char *s, int pos = 0) const ; |
8 | int find_first_of( const char *s, int pos, int n) const ; |
9 | int find_first_of( const string &s, int pos = 0) const ; |
11 | int find_first_not_of( char c, int pos = 0) const ; |
12 | int find_first_not_of( const char *s, int pos = 0) const ; |
13 | int find_first_not_of( const char *s, int pos, int n) const ; |
14 | int find_first_not_of( const string &s, int pos = 0) const ; |
16 | int find_last_of( char c, int pos = npos) const ; |
17 | int find_last_of( const char *s, int pos = npos) const ; |
18 | int find_last_of( const char *s, int pos, int n = npos) const ; |
19 | int find_last_of( const string &s, int pos = npos) const ; |
20 | int find_last_not_of( char c, int pos = npos) const ; |
21 | int find_last_not_of( const char *s, int pos = npos) const ; |
22 | int find_last_not_of( const char *s, int pos, int n) const ; |
23 | int find_last_not_of( const string &s, int pos = npos) const ; |
string类的替换函数:
1 | string &replace( int p0, int n0, const char *s); |
2 | string &replace( int p0, int n0, const char *s, int n); |
3 | string &replace( int p0, int n0, const string &s); |
4 | string &replace( int p0, int n0, const string &s, int pos, int n); |
5 | string &replace( int p0, int n0, int n, char c); |
6 | string &replace(iterator first0, iterator last0, const char *s); |
7 | string &replace(iterator first0, iterator last0, const char *s, int n); |
8 | string &replace(iterator first0, iterator last0, const string &s); |
9 | string &replace(iterator first0, iterator last0, int n, char c); |
10 | string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last); |
string类的插入函数:
1 | string &insert( int p0, const char *s); |
2 | string &insert( int p0, const char *s, int n); |
3 | string &insert( int p0, const string &s); |
4 | string &insert( int p0, const string &s, int pos, int n); |
6 | string &insert( int p0, int n, char c); |
7 | iterator insert(iterator it, char c); |
8 | void insert(iterator it, const_iterator first, const_iterator last); |
9 | void insert(iterator it, int n, char c); |
string类的删除函数
1 | iterator erase(iterator first, iterator last); |
2 | iterator erase(iterator it); |
3 | string &erase( int pos = 0, int n = npos); |
string类的迭代器处理:
string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。
用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:
1 | const_iterator begin() const ; |
3 | const_iterator end() const ; |
5 | const_iterator rbegin() const ; |
7 | const_iterator rend() const ; |
rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现
字符串流处理:
通过定义ostringstream和istringstream变量实现,#include <sstream>头文件中。例如:
1 | string input( "hello,this is a test" ); |
2 | istringstream is(input); |
以上就是对C++ string类的一个简要介绍。