(一)string类的构造函数:
string();
string( size_type length, char ch );
string( const char *str );
string( const char *str, size_type length );
string( string &str, size_type index, size_type length );
string( input_iterator start, input_iterator end );
字符串的构造函数创建一个新字符串,包括:
- 以length为长度的ch的拷贝(即length个ch)
- 以str为初值 (长度任意),
- 以index为索引开始的子串,长度为length, 或者
- 以从start到end的元素为初值.
e.g.:
string str1( 5, 'c' ); string str2( "Now is the time..." ); string str3( str2, 11, 4 ); cout << str1 << endl; cout << str2 << endl; cout << str3 << endl;
Output:
ccccc Now is the time... time
(二)string类的操作符:
== > < >= <= != + += []==, >, <, >=, <=, !=比较字符串.
+ , += 操作符连接两个字符串,
[]获取特定的字符
(三)string类的迭代器:
begin() 指向字符串的第一个元素,
end() 指向字符串的末尾(最后一个字符的下一个位置).
rbegin() 指向字符串的最后一个字符,(逆向迭代器)
rend() 指向字符串的开头(第一个字符的前一个位置).
(四)string类的一些常用函数
char* c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同.
size_type length()函数返回字符串长度.
bool empty()如果字符串为空则empty()返回真(true),否则返回假(false).
size_type copy( char *str, size_type num, size_type index )拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数
void swap(basic_string &str) 交换字符串内容.
erase()删除函数:
iterator erase( iterator pos );
iterator erase( iterator start, iterator end );
basic_string &erase( size_type index = 0, size_type num = npos );
iterator erase( iterator start, iterator end );
basic_string &erase( size_type index = 0, size_type num = npos );
erase()函数:
- 删除pos指向的字符, 返回指向下一个字符的迭代器,
- 删除从start到end的所有字符, 返回一个迭代器,指向被删除的最后一个字符的下一个位置
- 删除从index索引开始的num个字符, 返回*this.
find()查找函数:
size_type find( const basic_string &str, size_type index );size_type find( const char *str, size_type index );
size_type find( const char *str, size_type index, size_type length );
size_type find( char ch, size_type index );
find()函数:
- 返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos,
- 返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
- 返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos
basic_string &replace( size_type index, size_type num, const basic_string &str );
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,size_type num2 );
basic_string &replace( size_type index, size_type num, const char *str );
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
basic_string &replace( iterator start, iterator end, const basic_string &str );
basic_string &replace( iterator start, iterator end, const char *str );
basic_string &replace( iterator start, iterator end, const char *str, size_type num );
basic_string &replace( iterator start, iterator end, size_type num, char ch );
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,size_type num2 );
basic_string &replace( size_type index, size_type num, const char *str );
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
basic_string &replace( iterator start, iterator end, const basic_string &str );
basic_string &replace( iterator start, iterator end, const char *str );
basic_string &replace( iterator start, iterator end, const char *str, size_type num );
basic_string &replace( iterator start, iterator end, size_type num, char ch );
replace()函数:
- 用str中的num个字符替换本字符串中的字符,从index开始
- 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
- 用str中的num个字符(从index开始)替换本字符串中的字符
- 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
- 用num2个ch字符替换本字符串中的字符,从index开始
- 用str中的字符替换本字符串中的字符,迭代器start和end指示范围
- 用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围,
- 用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.
insert()插入函数:
iterator insert( iterator i, const char &ch );
basic_string &insert( size_type index, const basic_string &str );
basic_string &insert( size_type index, const char *str );
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
basic_string &insert( size_type index, const char *str, size_type num );
basic_string &insert( size_type index, size_type num, char ch );
void insert( iterator i, size_type num, const char &ch );
void insert( iterator i, iterator start, iterator end );
basic_string &insert( size_type index, const basic_string &str );
basic_string &insert( size_type index, const char *str );
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
basic_string &insert( size_type index, const char *str, size_type num );
basic_string &insert( size_type index, size_type num, char ch );
void insert( iterator i, size_type num, const char &ch );
void insert( iterator i, iterator start, iterator end );
insert()函数:
- 在迭代器i表示的位置前面插入一个字符ch,
- 在字符串的位置index插入字符串str,
- 在字符串的位置index插入字符串str的子串(从index2开始,长num个字符),
- 在字符串的位置index插入字符串str的num个字符,
- 在字符串的位置index插入num个字符ch的拷贝,
- 在迭代器i表示的位置前面插入num个字符ch的拷贝,
- 在迭代器i表示的位置前面插入一段字符,从start开始,以end结束.