C++ string类型详解

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, size_type num, const basic_string &str );//用str中的num个字符替换本字符串中的字符,从index开始
    basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
    size_type num2 );//用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
    basic_string &replace( size_type index, size_type num, const char *str );//用str中的num个字符(从index开始)替换本字符串中的字符
    basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );//用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
    basic_string &replace( size_type index, size_type num1, size_type num2, char ch );//用num2个ch字符替换本字符串中的字符,从index开始,num1个字符
    basic_string &replace( iterator start, iterator end, const basic_string &str );//用str中的字符替换本字符串中的字符,迭代器start和end指示范围
    basic_string &replace( iterator start, iterator end, const char *str );//用str替换本字符串中的内容,迭代器start和end指示范围
    basic_string &replace( iterator start, iterator end, const char *str, size_type num );//用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围
    basic_string &replace( iterator start, iterator end, size_type num, char ch );//用num个ch字符替换本字符串中的内容,迭代器start和end指示范围

查找
    以下各种find函数十分相似,所以就不一一注释了
    返回值:如果没找到则返回string::npos

find:
    size_type find( const basic_string &str, size_type index );//返回str在字符串中第一次出现的位置(从index开始查找)
    size_type find( const char *str, size_type index );//返回str在字符串中第一次出现的位置(从index开始查找)
    size_type find( const char *str, size_type index, size_type length );//返回str在字符串中第一次出现的位置(从index开始查找,长度为length)
    size_type find( char ch, size_type index );//返回字符ch在字符串中第一次出现的位置(从index开始查找)

find_first_of:查找在字符串中第一个与str中的某个字符匹配的字符
    size_type find_first_of( const basic_string &str, size_type index = 0 );
    size_type find_first_of( const char *str, size_type index = 0 );
    size_type find_first_of( const char *str, size_type index, size_type num );
    size_type find_first_of( char ch, size_type index = 0 );

find_first_not_of:在字符串中查找第一个与str中的字符都不匹配的字符                      
    size_type find_first_not_of( const basic_string &str, size_type index = 0 );
    size_type find_first_not_of( const char *str, size_type index = 0 );
    size_type find_first_not_of( const char *str, size_type index, size_type num );
    size_type find_first_not_of( char ch, size_type index = 0 );

find_last_of:在字符串中查找最后一个与str中的某个字符匹配的字符
  size_type find_last_of( const basic_string &str, size_type index = npos );
  size_type find_last_of( const char *str, size_type index = npos );
  size_type find_last_of( const char *str, size_type index, size_type num );
  size_type find_last_of( char ch, size_type index = npos );

find_last_not_of:在字符串中查找最后一个与str中的字符都不匹配的字符

    size_type find_last_not_of( const basic_string &str, size_type index = npos );
    size_type find_last_not_of( const char *str, size_type index = npos);
    size_type find_last_not_of( const char *str, size_type index, size_type num );
    size_type find_last_not_of( char ch, size_type index = npos );

rfind函数
  size_type rfind( const basic_string &str, size_type index );//返回最后一个与str中的某个字符匹配的字符,从index开始查找
  size_type rfind( const char *str, size_type index );//返回最后一个与str中的某个字符匹配的字符,从index开始查找
  size_type rfind( const char *str, size_type index, size_type num );//返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符
  size_type rfind( char ch, size_type index );//返回最后一个与ch匹配的字符,从index开始查找

at函数
     reference at( size_type index );//at()函数返回一个引用,指向在index位置的字符. 如果index不在字符串范围内, at() 将报告"out of range"错误,并抛出out_of_range异常

begin函数
    iterator begin();//begin()函数返回一个迭代器,指向字符串的第一个元素

end函数
    iterator end();//返回一个迭代器,指向字符串的末尾(最后一个字符的下一个位置)

c_str函数
    const char *c_str();//返回一个指向正规C字符串的指针, 内容与本字符串相同

capacity函数
    size_type capacity();//返回在重新申请更多的空间前字符串可以容纳的字符数. 这个数字至少与 size()一样大

copy函数
    size_type copy( char *str, size_type num, size_type index );//拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数

data函数
    const char *data();//返回指向自己的第一个字符的指针

empty函数
    bool empty();//如果字符串为空则empty()返回真(true),否则返回假(false)

get_allocator函数

    allocator_type get_allocator();//返回本字符串的配置器

length函数

    size_type length();//返回字符串的长度. 这个数字应该和size()返回的数字相同

max_size
    size_type max_size();//返回字符串能保存的最大字符数

rbegin函数
    rbegin();//返回一个逆向迭代器,指向最后一个字符

rend函数
    rend();//返回一个逆向迭代器,指向第一个元素的前一个位置

reserve函数
    reserve( size_type num );//保留一定容量以容纳字符串(设置capacity值)

resize函数
  void resize( size_type num );//改变本字符串的大小到num, 新空间的内容不确定
  void resize( size_type num, char ch );//也可以指定用ch填充

size函数
    size();//返回字符串中字符的数量

substr函数
     basic_string substr( size_type index, size_type num = npos );//返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串
 
swap函数
    void swap( basic_string &str );//把str和本字符串交换

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值