string的一些常用函數用法

頭文件:#include<string>

using namespace std;


一、構造函數

string(const char *s);    //用字符串s初始化
string(int n,char c);     //用n個字符c初始化


二、常用功能函數

1、int length()const;       //返回當前字符串的長度

2、void resize(int len,char c);//把字符串當前大小置爲len,並用字符c填充不足的部分

3、string &operator+=(const string &s);//把字符串s連接到當前字符串的結尾 

4、string &append(const char *s);            //把c類型字符串s連接到當前字符串結尾

5、string substr(int pos = 0,int n = npos) const;//返回pos開始的n個字符組成的字符串

6、void swap(string &s2);    //交換當前字符串與s2的值

7、int find(const char *s, int pos = 0) const;//從pos開始查找字符串s在當前串中的位置

8、int rfind(const string &s,int pos = npos) const;
//從pos開始從後向前查找字符串s中前n個字符組成的字符串在當前串中的位置,成功返回所在位置,失敗時返回string::npos的值 

9、string &replace(int p0, int n0,const char *s);//刪除從p0開始的n0個字符,然後在p0處插入串s

10、string &insert(int p0,const string &s, int pos, int n);
//前4個函數在p0位置插入字符串s中pos開始的前n個字符

11、iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字符,返回刪除後迭代器的位置

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章