std::string

 

標準模板庫(STL)提供了一個std::string類,其是std::basic_string的一個特化,它是一個容器類,可把字符串當作普通類型來使用,並支持比較、連接、遍歷、STL算法、複製、賦值等等操作,這個類定義在<string>頭文件中。

#include <string> //注意這裏不是string.h string.h是C字符串頭文件
1.聲明一個C++字符串
std::string類的構造函數

聲明一個字符串變量很簡單:
string Str;
這樣我們就聲明瞭一個字符串變量,但既然是一個類,就有構造函數和析構函數。上面的聲明沒有傳入參數,所以就直接使用了string的默認的構造函數,這個函數所作的就是把Str初始化爲一個空字符串。String類的構造函數和析構函數如下:
a) string s(); //生成一個空字符串s
b) string s(str) //拷貝構造函數 生成str的複製品string(const string& str)
c) string s(str,stridx) //將字符串str內"始於位置stridx"的部分當作字符串的初值
d) string s(const string& str, size_type pos,strlen) //將字符串str內"始於pos且長度頂多strlen"的部分作爲字符串的初值
e) string s(const char *s) //將C字符串作爲s的初值
f) string s(const char* cstr, size_type n) //使用字符串str的前n個字符初始化作爲字符串s的初值。
g) string s(int num,char c) //生成一個字符串,包含num個c字符
h) string s(beg,end) //以區間beg;end(不包含end)內的字符作爲字符串s的初值
i) s.~string() //銷燬所有字符,釋放內存

2.字符串操作函數
這裏是C++字符串的重點,我先把各種操作函數羅列出來,不喜歡把所有函數都看完的人可以在這裏找自己喜歡的函數,再到後面看他的詳細解釋。
a) =,assign() //賦以新值
b) swap() //交換兩個字符串的內容
c) +=,append(),push_back() //在尾部添加字符
d) insert() //插入字符
e) erase(int nStart,int nEnd) //刪除nStart—nEnd位置字符
f) clear() //刪除全部字符
g) replace() //替換字符
h) + //串聯字符串
i) ==,!=,<,<=,>,>=,compare() //比較字符串
j) size(),length() //返回字符數量
k) max_size() //返回字符的可能最大個數
l) empty() //判斷字符串是否爲空
m) capacity() //返回重新分配之前的字符容量
n) reserve() //保留一定量內存以容納一定數量的字符
o) [ ], at() //存取單一字符
p) >>,getline() //從stream讀取某值
q) << //將謀值寫入stream
r) copy() //將某值賦值爲一個C_string
s) c_str() //將內容以C_string返回
t) data() //將內容以字符數組形式返回
u) substr() //返回某個子字符串
v)查找函數
w)begin() end() //提供類似STL的迭代器支持
x) rbegin() rend() //逆向迭代器
y) get_allocator() //返回配置器



在平常工作中經常用到了string類,本人記憶了不好用到了的時候經常要去查詢。在網上摘抄一下總結一下,爲以後的查詢方便:

string類的構造函數:
string(const char*s);    //用c字符串s初始化string(intn,char c);     //用n個字符c初始化


string類的字符操作:
const char&operator[](int n)const;
const char &at(intn)const;
char &operator[](intn);
char &at(int n);
operator[]和at()均返回當前字符串中第n個字符的位置,但at函數提供範圍檢查,當越界時會拋出out_of_range異常,下標運算符[]不提供檢查訪問。
const char *data()const;//返回一個非null終止的c字符數組
const char *c_str()const;//返回一個以null終止的c字符串
int copy(char *s, int n,int pos = 0) const;//把當前串中以pos開始的n個字符拷貝到以s爲起始位置的字符數組中,返回實際拷貝的數目

注:對於string中對象字符的處理,有很多已有的函數在CCtype頭文件中,可以很方便的應用


string的特性描述:
intcapacity()const;    //返回當前容量(即string中不必增加內存即可存放的元素個數)
intmax_size()const;    //返回string對象中可存放的最大字符串的長度
intsize()const;        //返回當前字符串的大小
intlength()const;       //返回當前字符串的長度
boolempty()const;        //當前字符串是否爲空
void resize(int len,charc);//把字符串當前大小置爲len,並用字符c填充不足的部分

string類的輸入輸出操作:string類重載運算符operator>>用於輸入,同樣重載運算符operator<<用於輸出操作。
函數getline(istream &in,string &s);用於從輸入流in中讀取字符串到s中,以換行符'\n'分開。

string的賦值:
string &operator=(conststring &s);//把字符串s賦給當前字符串
string &assign(constchar *s);//用c類型字符串s賦值
string &assign(constchar *s,int n);//用c字符串s開始的n個字符賦值
string &assign(conststring &s);//把字符串s賦給當前字符串
string &assign(intn,char c);//用n個字符c賦值給當前字符串
string &assign(conststring &s,int start,int n);//把字符串s中從start開始的n個字符賦給當前字符串
string&assign(const_iterator first,const_itertor last);//把first和last迭代器之間的部分賦給字符串

string的連接:
string&operator+=(const string &s);//把字符串s連接到當前字符串的結尾 
string &append(constchar *s);            //把c類型字符串s連接到當前字符串結尾
string &append(constchar *s,int n);//把c類型字符串s的前n個字符連接到當前字符串結尾
string &append(conststring &s);    //同operator+=()
string &append(conststring &s,int pos,int n);//把字符串s中從pos開始的n個字符連接到當前字符串的結尾
string &append(intn,char c);        //在當前字符串結尾添加n個字符c
string&append(const_iterator first,const_iterator last);//把迭代器first和last之間的部分連接到當前字符串的結尾


string的比較:
bool operator==(conststring &s1,const string &s2)const;//比較兩個字符串是否相等
運算符">","<",">=","<=","!="均被重載用於字符串的比較;
int compare(const string&s) const;//比較當前字符串和s的大小
int compare(int pos, intn,const string &s)const;//比較當前字符串從pos開始的n個字符組成的字符串與s的大小
int compare(int pos, intn,const string &s,int pos2,int n2)const;//比較當前字符串從pos開始的n個字符組成的字符串與s中pos2開始的n2個字符組成的字符串的大小
int compare(const char *s)const;
int compare(int pos, intn,const char *s) const;
int compare(int pos, intn,const char *s, int pos2) const;
compare函數在>時返回1,<時返回-1,==時返回0   
string的子串:
string substr(int pos =0,int n = npos) const;//返回pos開始的n個字符組成的字符串

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


string類的查找函數:
int find(char c, int pos =0) const;//從pos開始查找字符c在當前字符串的位置
int find(const char *s, intpos = 0) const;//從pos開始查找字符串s在當前串中的位置
int find(const char *s, intpos, int n) const;//從pos開始查找字符串s中前n個字符在當前串中的位置
int find(const string&s, int pos = 0) const;//從pos開始查找字符串s在當前串中的位置
//查找成功時返回所在位置,失敗返回string::npos的值 
int rfind(char c, int pos =npos) const;//從pos開始從後向前查找字符c在當前串中的位置
int rfind(const char *s,int pos = npos) const;
int rfind(const char *s,int pos, int n = npos) const;
int rfind(const string&s,int pos = npos) const;
//從pos開始從後向前查找字符串s中前n個字符組成的字符串在當前串中的位置,成功返回所在位置,失敗時返回string::npos的值 
int find_first_of(char c,int pos = 0) const;//從pos開始查找字符c第一次出現的位置
int find_first_of(constchar *s, int pos = 0) const;
int find_first_of(constchar *s, int pos, int n) const;
int find_first_of(conststring &s,int pos = 0) const;
//從pos開始查找當前串中第一個在s的前n個字符組成的數組裏的字符的位置。查找失敗返回string::npos 
int find_first_not_of(charc, int pos = 0) const;
int find_first_not_of(constchar *s, int pos = 0) const;
int find_first_not_of(constchar *s, int pos,int n) const;
int find_first_not_of(conststring &s,int pos = 0) const;
//從當前串中查找第一個不在串s中的字符出現的位置,失敗返回string::npos 
int find_last_of(char c,int pos = npos) const;
int find_last_of(const char*s, int pos = npos) const;
int find_last_of(const char*s, int pos, int n = npos) const;
int find_last_of(conststring &s,int pos = npos) const; 
int find_last_not_of(charc, int pos = npos) const;
int find_last_not_of(constchar *s, int pos = npos) const;
int find_last_not_of(constchar *s, int pos, int n) const;
int find_last_not_of(conststring &s,int pos = npos) const;
//find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,只不過是從後向前查找


string類的替換函數:
string &replace(int p0,int n0,const char *s);//刪除從p0開始的n0個字符,然後在p0處插入串s
string &replace(int p0,int n0,const char *s, int n);//刪除p0開始的n0個字符,然後在p0處插入字符串s的前n個字符
string &replace(int p0,int n0,const string &s);//刪除從p0開始的n0個字符,然後在p0處插入串s
string &replace(int p0,int n0,const string &s, int pos, int n);//刪除p0開始的n0個字符,然後在p0處插入串s中從pos開始的n個字符
string &replace(int p0,int n0,int n, char c);//刪除p0開始的n0個字符,然後在p0處插入n個字符c
string&replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之間的部分替換爲字符串s
string&replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之間的部分替換爲s的前n個字符
string&replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之間的部分替換爲串s
string&replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之間的部分替換爲n個字符c
string&replace(iterator first0, iterator last0,const_iterator first,const_iterator last);//把[first0,last0)之間的部分替換成[first,last)之間的字符串


string類的插入函數: string &insert(int p0, const char *s);
string &insert(int p0,const char *s, int n);
string &insert(intp0,const string &s);
string &insert(intp0,const string &s, int pos, int n);
//前4個函數在p0位置插入字符串s中pos開始的前n個字符
string &insert(int p0,int n, char c);//此函數在p0處插入n個字符c
iterator insert(iteratorit, char c);//在it處插入字符c,返回插入後迭代器的位置
void insert(iterator it,const_iterator first, const_iterator last);//在it處插入[first,last)之間的字符
void insert(iterator it,int n, char c);//在it處插入n個字符c


string類的刪除函數 iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字符,返回刪除後迭代器的位置
iterator erase(iteratorit);//刪除it指向的字符,返回刪除後迭代器的位置
string &erase(int pos =0, int n = npos);//刪除pos開始的n個字符,返回修改後的字符串


string類的迭代器處理:
string類提供了向前和向後遍歷的迭代器iterator,迭代器提供了訪問各個字符的語法,類似於指針操作,迭代器不檢查範圍。
用string::iterator或string::const_iterator聲明迭代器變量,const_iterator不允許改變迭代的內容。常用迭代器函數有:
const_iteratorbegin()const;
iteratorbegin();               //返回string的起始位置
const_iterator end()const;
iteratorend();                   //返回string的最後一個字符後面的位置
const_iteratorrbegin()const;
iteratorrbegin();               //返回string的最後一個字符的位置
const_iterator rend()const;
iteratorrend();                   //返回string第一個字符位置的前面
rbegin和rend用於從後向前的迭代訪問,通過設置迭代器string::reverse_iterator,string::const_reverse_iterator實現


字符串流處理:
通過定義ostringstream和istringstream變量實現,<sstream>頭文件中
例如:
    stringinput("hello,this is a test");
   istringstream is(input);
    strings1,s2,s3,s4;
   is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
   ostringstream os;
   os<<s1<<s2<<s3<<s4;
   cout<<os.str();

標準C++庫字符串類std::string的用法

#include<string>

std::string s1;

std::string s3(s2);

std::string s2("this is a string");

begin       得到指向字符串開頭的Iterator

end       得到指向字符串結尾的Iterator

rbegin       得到指向反向字符串開頭的Iterator

rend       得到指向反向字符串結尾的Iterator

size       得到字符串的大小

length()       size函數功能相同

max_size       字符串可能的最大大小

capacity       在不重新分配內存的情況下,字符串可能的大小

empty       判斷是否爲空

operator[]       取第幾個元素,相當於數組

c_str       取得C風格的constchar*字符串

data       取得字符串內容地址

operator=       賦值操作符

reserve       預留空間

swap       交換函數

insert       插入字符

append       追加字符

push_back       追加字符

erase       刪除字符串

clear       清空字符容器中所有內容

resize       重新分配空間

assign       和賦值操作符一樣

replace       替代

copy       字符串到空間

find       查找,返回基於0的索引號

rfind       反向查找

find_first_of       查找包含子串中的任何字符,返回第一個位置

find_first_not_of       查找不包含子串中的任何字符,返回第一個位置

find_last_of       查找包含子串中的任何字符,返回最後一個位置

find_last_not_of       查找不包含子串中的任何字符,返回最後一個位置

substr(n1,len)       得到字符串從n1開始的長度爲len的子串

比較字符串(支持所有的關係運算符)

compare       比較字符串

operator+       字符串鏈接

operator+=       += 操作符

operator==       判斷是否相等

operator!=       判斷是否不等於

operator<       判斷是否小於

operator>>       從輸入流中讀入字符串

operator<<       字符串寫入輸出流

getline       從輸入流中讀入一行



2.3元素存取

我們可以使用下標操作符[]和函數at()對元素包含的字符進行訪問。但是應該注意的是操作符[]並不檢查索引是否有效(有效索引0~str.length()),如果索引失效,會引起未定義的行爲。而at()會檢查,如果使用 at()的時候索引無效,會拋出out_of_range異常。
有一個例外不得不說,const string a;的操作符[]對索引值是a.length()仍然有效,其返回值是’’。其他的各種情況,a.length()索引都是無效的。舉例如下:
const string Cstr("const string");
string Str("string");

Str[3]; //ok
Str.at(3); //ok

Str[100]; //未定義的行爲
Str.at(100); //throw out_of_range

Str[Str.length()] //未定義行爲
Cstr[Cstr.length()] //返回 ‘’
Str.at(Str.length());//throw out_of_range
Cstr.at(Cstr.length()) ////throw out_of_range

我不贊成類似於下面的引用或指針賦值:
char& r=s[2];
char* p= &s[3];
因爲一旦發生重新分配,r,p立即失效。避免的方法就是不使用。


遍歷所有字符,這可由C風格的索引或STL迭代子來完成(如果無需修改,應使用const_iterator)。

std::string name = "marius";

for(size_t i = 0; i < name.length(); ++i)

std::cout << name[i];

 

for(std::string::const_iterator cit = name.begin(); cit != name.end(); ++cit)

std::cout << *cit;

 

for(std::string::iterator it = name.begin();it != name.end(); ++it)

*it = toupper(*it);
2.4比較函數
C ++字符串支持常見的比較操作符(>,>=,<,<=,==,!=),甚至支持string與C-string的比較(如 str<"hello")。在使用>,>=,<,<=這些操作符的時候是根據"當前字符特性"將字符按字典順序進行逐一得 比較。字典排序靠前的字符小,比較的順序是從前向後比較,遇到不相等的字符就按這個位置上的兩個字符的比較結果確定兩個字符串的大小。同時,string ("aaaa") <string(aaaaa)。
另一個功能強大的比較函數是成員函數compare()。他支持多參數處理,支持用索引值和長度定位子串來進行比較。他返回一個整數來表示比較結果,返回值意義如下:0-相等 〉0-大於 <0-小於。舉例如下:
string s("abcd");

s.compare("abcd"); //返回0
s.compare("dcba"); //返回一個小於0的值
s.compare("ab"); //返回大於0的值

s.compare(s); //相等
s.compare(0,2,s,2,2); //用"ab"和"cd"進行比較 小於零
s.compare(1,2,"bcx",2); //用"bc"和"bc"比較。
怎麼樣?功能夠全的吧!什麼?還不能滿足你的胃口?好吧,那等着,後面有更個性化的比較算法。先給個提示,使用的是STL的比較算法。什麼?對STL一竅不通?靠,你重修吧!

2.5 更改內容
這在字符串的操作中佔了很大一部分。

首先講賦值,第一個賦值方法當然是使用操作符=,新值可以是string(如:s=ns) 、c_string(如:s="gaint")甚至單一字符(如:s=’j’)。還可以使用成員函數assign(),這個成員函數可以使你更靈活的對字符串賦值。還是舉例說明吧:
s.assign(str); //不說
s.assign(str,1,3);//如果str是"iamangel" 就是把"ama"賦給字符串
s.assign(str,2,string::npos);//把字符串str從索引值2開始到結尾賦給s
s.assign("gaint"); //不說
s.assign("nico",5);//把’n’ ‘I’ ‘c’ ‘o’ ‘’賦給字符串
s.assign(5,’x’);//把五個x賦給字符串
把字符串清空的方法有三個:s="";s.clear();s.erase();(我越來越覺得舉例比說話讓別人容易懂!)。
string提供了很多函數用於插入(insert)、刪除(erase)、替換(replace)、增加字符。
先說增加字符(這裏說的增加是在尾巴上),函數有 +=、append()、push_back()。舉例如下:
s+=str;//加個字符串
s+="my name is jiayp";//加個C字符串
s+=’a’;//加個字符

s.append(str);
s.append(str,1,3);//不解釋了 同前面的函數參數assign的解釋
s.append(str,2,string::npos)//不解釋了

s.append("my name is jiayp");
s.append("nico",5);
s.append(5,’x’);
2.5.1、在字符串結尾插入其他元素。
s.push_back(‘a’);//這個函數只能增加單個字符 對STL熟悉的理解起來很簡單
2.5.2、 在指定位置插入字符串或字符。

也許你需要在string中間的某個位置插入字符串,這時候你可以用insert()函數,這個函數需要你指定一個安插位置的索引,被插入的字符串將放在這個索引的後面。
s.insert(0,"my name");
s.insert(1,str);
這 種形式的insert()函數不支持傳入單個字符,這時的單個字符必須寫成字符串形式(讓人噁心)。既然你覺得噁心,那就不得不繼續讀下面一段話:爲了插 入單個字符,insert()函數提供了兩個對插入單個字符操作的重載函數:insert(size_type index,size_type num,chart c)和insert(iterator pos,size_type num,chart c)。其中size_type是無符號整數,iterator是char*,所以,你這麼調用insert函數是不行的:insert(0,1, ’j’);這時候第一個參數將轉換成哪一個呢?所以你必須這麼寫:insert((string::size_type)0,1,’j’)!第二種形式指 出了使用迭代器安插字符的形式,在後面會提及。順便提一下,string有很多操作是使用STL的迭代器的,他也儘量做得和STL靠近。
刪除函數erase()的形式也有好幾種(真煩!),替換函數replace()也有好幾個。舉例吧:
string s="il8n";
s.replace(1,2,"nternationalizatio");//從索引1開始的2個替換成後面的C_string

2.5.3、 刪除字符串的某一部分。
s.erase(13);//從索引13開始往後全刪除
s.erase(7,5);//從索引7開始往後刪5個

2.6提取子串和字符串連接
題取子串的函數是:substr(),形式如下:
s.substr();//返回s的全部內容
s.substr(11);//從索引11往後的子串
s.substr(5,6);//從索引5開始6個字符
把兩個字符串結合起來的函數是+。(誰不明白請致電120)

2.7輸入輸出操作
1.>> 從輸入流讀取一個string。
2.<< 把一個string寫入輸出流。
另一個函數就是getline(),他從輸入流讀取一行內容,直到遇到分行符或到了文件尾。

2.8搜索與查找

std::string類的查找函數
查找函數很多,功能也很強大,包括了:
find()
rfind()
find_first_of()
find_last_of()
find_first_not_of()
find_last_not_of()

 
這些函數返回符合搜索條件的字符區間內的第一個字符的索引,沒找到目標就返回npos。所有的函數的參數說明如下:
第一個參數是被搜尋的對象。第二個參數(可有可無)指出string內的搜尋起點索引,第三個參數(可有可無)指出搜尋的字符個數。比較簡單,不多說不理解的可以向我提出,我再仔細的解答。當然,更加強大的STL搜尋在後面會有提及。
最 後再說說npos的含義,string::npos的類型是string::size_type,所以,一旦需要把一個索引與npos相比,這個索引值必須是string::size)type類型的,更多的情況下,我們可以直接把函數和npos進行比較(如:if(s.find("jia")== string::npos))。

3.1、使用STL算法

 

std::string name = "marius";

// 使字符串全爲大寫

std::transform(name.begin(), name.end(), name.begin(),toupper);

std::string name = "marius";

// 升序排列字符串

std::sort(name.begin(), name.end());

std::string name = "marius";

// 反轉字符串

std::reverse(name.begin(), name.end());

bool iswhitespace(char ch)

{

return  ch == ' ' || ch == 't' || ch == 'v' ||

ch == 'r' || ch == 'n';

}

 

std::string name = " marius  ";

// 刪除空白字符

std::string::iterator newend = std::remove_if(name.begin(), name.end(), iswhitespace);

name.erase(newend);

 

 

std::string類的替換函數

 

函數1:
std::string & replace(size_type pos1, size_type n1, const std::string & str, size_type pos2 = 0, size_type n2 = npos);

該函數的作用:使用str字符串從位置pos2開始的n2個字符,替換當前字符串從pos1位置開始處的n1個字符。
可以這樣理解:該函數將當前字符串從pos1開始的n1個字符全部刪除,然後再用str整個字符串或者str從pos2開始的n2個字符,從pos1位置開始填入到當前字符串中。

提醒:如果n1或者n2的數值超出了對應字符串的長度,以實際長度爲準,不會出現訪問越界的情況。

注意:
a、如果pos1指定的位置超出當前字符串的範圍,拋出std::out_of_range異常,不捕捉將導致coredump。
b、如果pos2指定的位置超出替換字符串str的範圍,拋出std::out_of_range異常,不捕捉將導致coredump。

函數2:
std::string& replace(size_type pos, size_type n1, const char * s, size_type n2);

該函數的作用:使用字符串s的前n2個字符,替換當前字符串從pos位置開始處的n1個字符。
可以這樣理解:函數將當前字符串從pos開始的n1個字符全部刪除,然後再用字符串s的前n2個字符填入到當前字符串中。類似於函數1的pos2等於0,必須指定n2的這種情況,但也有一點的差別,下面會注意裏描述這種差別。
注意:
a、如果pos指定的位置超出當前字符串的範圍,拋出std::out_of_range異常,不捕捉將導致coredump。
b、該函數不會判斷字符串s和n2的大小關係,它嚴格地從s起始處拷貝n2個字符到指定位置。如果n2表示的長度超出了s的範圍,它會讀取s後面的內存空間,有可能會因爲內存訪問越界而coredump。但函數1的n2可以超出範圍,它以實際長度爲準。

函數3:
std::string& replace(size_type pos, size_type n1, const char* s);

該函數的作用:使用以''爲結尾的字符串s,替換當前字符串從pos位置開始處的n1個字符。
可以這樣理解:函數將當前字符串從pos開始的n1個字符全部刪除,然後再用字符串s從開始到以''結束的所有字符,從pos位置開始填入到當前字符串中。

注意:如果pos指定的位置超出當前字符串的範圍,拋出std::out_of_range異常,不捕捉將導致coredump。

函數4:
std::string& replace(size_type pos, size_type n1, size_type n2, char c);

該函數的作用:使用n2個c表示的字符,替換當前字符串從pos位置開始處的n1個字符。
可以這麼理解:函數將當前字符串從pos開始的n1個字符全部刪除,然後再用n2個c字符,從pos位置開始填入到當前字符串中。

注意:如果pos指定的位置超出當前字符串的範圍,拋出std::out_of_range異常,不捕捉將導致coredump。

函數5:
std::string& replace(iterator i1, iterator i2, const std::string& str);

該函數的作用:使用字符串str,替換當前字符串[i1,i2)之間的字符。

函數6:
std::string& replace(iterator i1, iterator i2, const char* s, size_type n);

該函數的作用:使用字符串s的前n個字符,替換當前字符串[i1,i2)之間的字符。

函數7:
std::string& replace(iterator i1, iterator i2, const char* s);

該函數的作用:使用以''結尾的字符串s,替換當前字符串[i1,i2)之間的字符。

函數8:
std::string& replace(iterator i1, iterator i2, size_type n, char c);

該函數的作用:使用n個c表示的字符,替換當前字符串[i1,i2)之間的字符。

std::string類的內部類型定義

typedef traits traits_type;

typedef typename traits::char_type value_type;

typedef size_t size_type;

typedef Allocator allocator_type;

typedef ptrdiff_t difference_type;

typedef Allocator allocator_type;

typedef Allocator allocator_type;

typedef Allocator allocator_type;

typedef charT& reference; typedef const charT& const_reference; typedef charT* pointer; typedef const charT* const_pointer; typedef pointer iterator; typedef const_pointer const_iterator; typedef ::reverse_iterator reverse_iterator; typedef ::reverse_iterator const_reverse_iterator; static const size_type npos = static_cast(-1);

std::string類的賦值運算符

string& operator= (const char* s);
string& operator= (char c);
string& operator+= (const string& rhs);
string& operator+= (const char* s);
string& operator+= (char c);
string operator+ (const string & lhs, const string & rhs);
string operator+ (const char* lhs, const string & rhs);
string operator+ (char lhs, const string & rhs);
string operator+ (const string & lhs, const char* rhs);
string operator+ (const string & lhs, char rhs);

std::string類的邏輯運算符

bool operator== (const string & lhs, const string & rhs);
bool operator== (const char* lhs, const string & rhs);
bool operator== (const string & lhs, const char* rhs);
bool operator!= (const string & lhs, const string & rhs);
bool operator!= (const char* lhs, const string & rhs);
bool operator!= (const string & lhs, const char* rhs);
bool operator< (const string & lhs, const string & rhs);
bool operator< (const char* lhs, const string & rhs);
bool operator< (const string & lhs, const char* rhs);
bool operator> (const string & lhs, const string & rhs);
bool operator> (const char* lhs, const string & rhs);
bool operator> (const string & lhs, const char* rhs);
bool operator<= (const string & lhs, const string & rhs);
bool operator<= (const char * lhs, const string & rhs);
bool operator<= (const string & lhs, const char * rhs);
bool operator>= (const string & lhs, const string & rhs);
bool operator>= (const char* lhs, const string & rhs);
bool operator>= (const string & lhs, const char* rhs);

istream& operator >> (istream&, string &);
ostream& operator << (ostream&, const string &);
istream& getline (istream&, string &, char delim = 'n');

 








         在程序中常常需要處理字符串,除了以前寫的一些關於char的方法的總結外,很多的時候也會用到string來進行字符串處理。下面對它的常用方法做些總結:
 
1、定義:
string &operator=(const string &s);//把字符串s賦給當前字符串
string &assign(const char *s);//用c類型字符串s賦值
string &assign(const char *s,int n);//用c字符串s開始的n個字符賦值
string &assign(const string &s);//把字符串s賦給當前字符串
string &assign(int n,char c);//用n個字符c賦值給當前字符串
string &assign(const string &s,int start,int n);//把字符串s中從start開始的n個字符賦給當前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之間的部分賦給字符串
 
2、append方法:
string &operator+=(const string &s);//把字符串s連接到當前字符串的結尾 
string &append(const char *s);            //把c類型字符串s連接到當前字符串結尾
string &append(const char *s,int n);//把c類型字符串s的前n個字符連接到當前字符串結尾
string &append(const string &s);    //同operator+=()
string &append(const string &s,int pos,int n);//把字符串s中從pos開始的n個字符連接到當前字符串的結尾
string &append(int n,char c);        //在當前字符串結尾添加n個字符c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之間的部分連接到當前字符串的結尾
 
3、比較compar方法:
bool operator==(const string &s1,const string &s2)const;//比較兩個字符串是否相等
運算符">","<",">=","<=","!="均被重載用於字符串的比較;
int compare(const string &s) const;//比較當前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比較當前字符串從pos開始的n個字符組成的字符串與s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比較當前字符串從pos開始的n個字符組成的字符串與s中pos2開始的n2個字符組成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函數在>時返回1,<時返回-1,==時返回0  
 
4、查找find方法:
int find(char c, int pos = 0) const;//從pos開始查找字符c在當前字符串的位置
int find(const char *s, int pos = 0) const;//從pos開始查找字符串s在當前串中的位置
int find(const char *s, int pos, int n) const;//從pos開始查找字符串s中前n個字符在當前串中的位置
int find(const string &s, int pos = 0) const;//從pos開始查找字符串s在當前串中的位置
//查找成功時返回所在位置,失敗返回string::npos的值 

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

int find_first_of(char c, int pos = 0) const;//從pos開始查找字符c第一次出現的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//從pos開始查找當前串中第一個在s的前n個字符組成的數組裏的字符的位置。查找失敗返回string::npos 

int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//從當前串中查找第一個不在串s中的字符出現的位置,失敗返回string::npos 

int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const; 

int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,只不過是從後向前查找
 
5、插入insert方法:
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4個函數在p0位置插入字符串s中pos開始的前n個字符
string &insert(int p0, int n, char c);//此函數在p0處插入n個字符c
iterator insert(iterator it, char c);//在it處插入字符c,返回插入後迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it處插入[first,last)之間的字符
void insert(iterator it, int n, char c);//在it處插入n個字符c
 
6、刪除erase方法:
iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字符,返回刪除後迭代器的位置
iterator erase(iterator it);//刪除it指向的字符,返回刪除後迭代器的位置
string &erase(int pos = 0, int n = npos);//刪除pos開始的n個字符,返回修改後的字符串
 
7、替換replace方法:
string &replace(int p0, int n0,const char *s);//刪除從p0開始的n0個字符,然後在p0處插入串s
string &replace(int p0, int n0,const char *s, int n);//刪除p0開始的n0個字符,然後在p0處插入字符串s的前n個字符
string &replace(int p0, int n0,const string &s);//刪除從p0開始的n0個字符,然後在p0處插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//刪除p0開始的n0個字符,然後在p0處插入串s中從pos開始的n個字符
string &replace(int p0, int n0,int n, char c);//刪除p0開始的n0個字符,然後在p0處插入n個字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之間的部分替換爲字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之間的部分替換爲s的前n個字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之間的部分替換爲串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之間的部分替換爲n個字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之間的部分替換成[first,last)之間的字符串 
8、獲得字串substr方法:
string substr(int pos = 0,int n = npos) const;//返回pos開始的n個字符組成的字符串
 
9、交換swap的方法:
void swap(string &s2);    //交換當前字符串與s2的值

 

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