string类

 

函数名 描述
begin 得到指向字符串开头的Iterator
end 得到指向字符串结尾的Iterator
rbegin 得到指向反向字符串开头的Iterator
rend 得到指向反向字符串结尾的Iterator
size 得到字符串的大小
length 和size函数功能相同
max_size 字符串可能的最大大小
capacity 在不重新分配内存的情况下,字符串可能的大小
empty 判断是否为空
operator[] 取第几个元素,相当于数组
c_str 取得C风格的const char* 字符串
data 取得字符串内容地址
operator= 赋值操作符
reserve 预留空间
swap 交换函数
insert 插入字符
append 追加字符
push_back 追加字符
operator+= += 操作符
erase 删除字符串
clear 清空字符容器中所有内容
resize 重新分配空间
assign 和赋值操作符一样
replace 替代
copy 字符串到空间
find 查找
rfind 反向查找
find_first_of 查找包含子串中的任何字符,返回第一个位置
find_first_not_of 查找不包含子串中的任何字符,返回第一个位置
find_last_of 查找包含子串中的任何字符,返回最后一个位置
find_last_not_of 查找不包含子串中的任何字符,返回最后一个位置
substr 得到字串
compare 比较字符串
operator+ 字符串链接
operator== 判断是否相等
operator!= 判断是否不等于
operator< 判断是否小于
operator>> 从输入流中读入字符串
operator<< 字符串写入输出流
getline 从输入流中读入一行

1.字符串反序函数:  reverse(iterator  begin,iterator  end);    头文件:<algorithm>

                          注:该函数对vector,list也适用。

样例代码: 

 

     string    strA("helloworld");           //在该日志中只定义这一次

     reverse(strA.begin(),strA.end()); 

 

2.将字符串内各字母排序

 

   样例代码:

 

   sort(strA.begin(),strA.end(),greater<char>());或  sort(strA.begin(),strA.end());

 

3.c_str 用法:

 

   样例代码:

 

    const char*   strB;

    strB=strA.c_str();

    cout<<strB<<endl;

 

    注:data用法与c_str 用法相同。

 

4.swap用法:

 

   样例代码:

 

   string  strB("hello world");

    strA.swap(strB);

   

5.insert用法:

 

  

string& insert ( size_t pos1, const string& str );
在原字符串的pos1位置(从0开始)插入字符串 str  

样例:

          strA=strA.insert(2,strB);

 

string& insert ( size_t pos1, const string& str, size_t pos2, size_t n );
Inserts a copy of a substring of str at character position pos1. The substring is the portion of str that begins at the character position pos2 and takes up to n characters (it takes less than n if the end of str is reached before).

样例:

          strA=strA.insert(2,strB,0,3);

 

void insert ( iterator p, size_t n, char c );
Inserts a string formed by the repetition of character c, n times, at the position referred by iterator p

样例:

          string::iterator     it;

          it=strA.begin()+1;
          strA.insert(it,3,'A');

 

iterator insert ( iterator p, char c );
Inserts a copy of character c at the position referred by iterator p and returns an iterator referring to this position where it has been inserted

样例:

          string::iterator     it;

          it=strA.insert(strA.begin()+1,'A');
          strA.insert(it+2,strA.begin(),strA.end());

          输出stA:      aAlaAlgorithmgorithm

 

6.  append和push_back:向字符串尾部插入元素

 

     strA.append(int  nTimes,char  c);

     strA.push_back(char   c);

 

7.erase用法:

   strA=strA.erase(int pos1,int pos2);         删除位置2到位置5的字符;

 

   strA.erase(iterator  itP);                          删除itP所指向的字符;

 

   strA.erase(iterator  itP,iterator itP1);     删除itP和itP1之间的字符;

 

8.replace 用法:

 

  

string& replace ( size_t pos1, size_t n1, const string& str );
string& replace ( iterator i1, iterator i2, const string& str );
The section is replaced by a copy of the entire string object str.
string& replace ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 );
The section is replaced by a copy of a substring of str. The substring is the portion of str that begins at the character position pos2 and takes up to n2 characters (it takes less than n2 if the end of the string is reached before).
string& replace ( size_t pos1, size_t n1, const char * s, size_t n2 );
string& replace ( iterator i1, iterator i2, const char * s, size_t n2 );
The section is replaced by a copy of the string formed by the first n2 characters in the array of characters pointed by s.
string& replace ( size_t pos1, size_t n1, const char * s );
string& replace ( iterator i1, iterator i2, const char * s );
The section is replaced by a copy of the string formed by the null-terminated character sequence (C string) pointed by s. The length of this caracter sequence is determined by the first ocurrence of a null character (as determined by traits.length(s)).
string& replace ( size_t pos1, size_t n1, size_t n2, char c );
string& replace ( iterator i1, iterator i2, size_t n2, char c );
The section is replaced by a repetition of character c, n2 times

样例代码:

       strA.replace(2,5,strB);

 

9.resize用法

   resize(int  nSize,char  cA);   :字符串长度扩大到nSize,新增的字符都是cA

 

   resize(int  nSize);                :字符串长度缩短到nSize.

 

10.rfind  用法

 

   size_t rfind ( const string& str, size_t pos = npos ) const;
   size_t rfind ( const char* s, size_t pos, size_t n ) const;
   size_t rfind ( const char* s, size_t pos = npos ) const;
   size_t rfind ( char c, size_t pos = npos ) const;

  

   注:size_t表示位置,str是匹配目标字符串,npos是查找的最后位置(表示查找范围)

 

   样例代码:

   #include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("The sixth sick sheik's sixth sheep's sick.");
  string key ("sixth");
  size_t found;

  found=str.rfind(key);
  if (found!=string::npos)
    str.replace (found,key.length(),"seventh");

  cout << str << endl;

  return 0;
}

  

11.substr用法:

 

     string substr ( size_t pos = 0, size_t n = npos ) const;

   注: pos为被选字串的起始位置,npos为被选字串所含字符个数。

12.swap用法:

   swap(string  str);     :母串与str交换内容,该函数无返回值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值