C++ string类型详解(一)

2014-11-24 11:53:47 · 作者: · 浏览: 4

string是非常强大的类型,很好的封装了字符串的操作,有些时候我们可以把string当做字符的容器,string也支持大多数容器操作,下面就列出string类型所支持的所有操作,本文并不是为了讲解string的用法和应用,而是希望作为string类型的参考文档,每个函数皆在注释后有详细说明,需要用时查阅即可。

string操作如下:

构造函数:
string();//空串
string( size_type length, char ch );//以length为长度的ch的拷贝(即length个ch)
string( const char *str );//以str为初值 (长度任意)
string( const char *str, size_type length );//同上,长度不限,但注意不要越界,以免发生不可预知问题
string( string &str, size_type index, size_type length );//以index为索引开始的子串,长度为length, 或者小于length
string( input_iterator start, input_iterator end );//以从start到end的元素为初值

支持的操作符:
==
>
<
>=
<=
!=
+
+=
[]

追加文本(append)
basic_string &append( const basic_string &str );//在字符串的末尾添加str
basic_string &append( const char *str );//在字符串末尾添加str所指向的c风格字符串
basic_string &append( const basic_string &str, size_type index, size_type len );//在字符串的末尾添加str的子串,子串以index索引开始,长度为len
basic_string &append( const char *str, size_type num );//在字符串的末尾添加str中的num个字符
basic_string &append( size_type num, char ch );//在字符串的末尾添加num个字符ch
basic_string &append( input_iterator start, input_iterator end );//在字符串的末尾添加以迭代器start和end表示的字符序列

赋值(assign)
basic_string &assign( const basic_string &str );//用str为字符串赋值
basic_string &assign( const char *str );//用str c风格为字符串赋值
basic_string &assign( const char *str, size_type num );//用str的开始num个字符为字符串赋值
basic_string &assign( const basic_string &str, size_type index, size_type len );//用str的子串为字符串赋值,子串以index索引开始,长度为len
basic_string &assign( size_type num, char ch );//用num个字符ch为字符串赋值

比较(compare)
int compare( const basic_string &str );//比较自己和str
int compare( const char *str );//比较自己和str
int compare( size_type index, size_type length, const basic_string &str );//比较自己的子串和str,子串以index索引开始,长度为length
int compare( size_type index, size_type length, const basic_string &str, size_type index2,
size_type length2 );//比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
int compare( size_type index, size_type length, const char *str, size_type length2 );//比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length
返回值 情况

小于零 this < str
零 this == str
大于零 this > str

删除(erase)
iterator erase( iterator pos );//删除pos指向的字符, 返回指向下一个字符的迭代器
iterator erase( iterator start, iterator end );//删除从start到end的所有字符, 返回一个迭代器,指向被删除的最后一个字符的下一个位置
basic_string &erase( size_type index = 0, size_type num = npos );//删除从index索引开始的num个字符, 返回*this

插入(insert)
iterator insert( iterator i, const char &ch );//在迭代器i表示的位置前面插入一个字符ch
basic_string &insert( size_type index, const basic_string &str );//在字符串的位置index插入字符串str
basic_string &insert( size_type index, const char *str );//在字符串的位置index插入字符串str
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );//在字符串的位置index插入字符串str的子串(从index2开始,长num个字符)
basic_string &insert( size_type index, const char *str, size_type num );//在字符串的位置index插入字符串str的num个字符
basic_string &insert( size_type index, size_type num, char ch );//在字符串的位置index插入num个字符ch的拷贝
void insert( iterator i, size_type num, const char &ch );//在迭代器i表示的位置前面插入num个字符ch的拷贝
void insert( iterator i, iterator start, iterator end );//在迭代器i表示的位置前面插入一段字符,从start开始,以end结束

替换(replace)
basic_string &replace( size_type index