轉自:http://www.cnblogs.com/ffhajbq/archive/2012/06/01/2529354.html
C++及C中的 string char指針及char數組
C++中string是標準庫中一種容器,相當於保存元素類型爲char的vector容器(自己理解),這個類提供了相當豐富的函數來完成對字符串操作,以及與C風格字符串之間轉換,下面是對string一些總結<引用>
一,C語言的字符串
在C語言裏,對字符串的處理一項都是一件比較痛苦的事情,因爲通常在實現字符串的操作的時候都會用到最不容易駕馭的類型——指針。
比如下面這個例子:
//example 1:
char str[12] = "Hello";
char *p = str;
*p = 'h'; //改變第一個字母
//example 2:
char *ptr = "Hello";
*ptr = 'h'; //錯誤
第一個字符串時用數組開闢的,它是可以改變的變量。而第二個字符串則是一個常量,也就是字面值。ptr只是指向它的指針而已,而不能改變指向的內容。
看兩者的彙編即可明瞭:
char p[] = "Hello";
004114B8 mov eax,dword ptr [string "Hello" (4166FCh)]
004114BD mov dword ptr [ebp-10h],eax
004114C0 mov cx,word ptr ds:[416700h]
004114C7 mov word ptr [ebp-0Ch],cx
char *ptr = "Hello";
004114CB mov dword ptr [ebp-1Ch],offset string "Hello" (4166FCh)
可見用數組和用指針是完全不相同的。
要想通過指針來改變常量是錯誤,正確的寫法應該是用const指針。
const char *ptr = "Hello";
二,初識string類
正是因爲C風格字符串(以空字符結尾的字符數組)太過複雜難於掌握,不適合大程序的開發,所以C++標準庫定義了一種string類,定義在頭文 件<string>。注意<string.h>和<cstring>都是錯誤的,這兩個頭文件主要定義C風格字符串 操作的一些方法,譬如strlen(), strcpy()等。第一個是C的頭文件格式,而第二個是C++風格的頭文件,但是和<string.h>是一樣的,它的目的是爲了和C兼 容。
看下面例子:
//example 3:
string str("world"); //可以用C風格字符串初始化
string words = "Hello";
string greet = words;
string join = greet + words; //可以像基本類型一樣操作
但是如果試圖把string類型的對象直接賦給C風格的字符串的話,編譯器會報錯的。
string var = "Olympic";
char *ptr = var; //error!
但是實際應用中這個問題也難以避免,很多時候我們還是需要將string類型的轉化爲char*來實現自定義的操作,C++標準庫也爲了和之前用C寫的程序兼容,於是可以用string的c_str()函數。
string var = "Olympic";
char *ptr = var.c_str(); //還不能被編譯
但是c_str()爲了防止意外地修改string對象,返回的是const指針,所以上面這段代碼是不能被編譯的。正確的應該是用const指針。
string var = "Olympic";
const char *p = var.c_str(); //Correct!
這個c_str()方法在C++IO流操作上也被廣泛應用。
在打開文件時,如果要指定文件名,可以用C風格的字符串。如果用到string類型的字符串作爲文件名時,就必須調用c_str()方法將其轉換爲一個C風格字符串。
//example 4:
string filename; //定義文件名稱
cin >> filename;
ifstream.open(filename.c_str()); //要使用C風格字符串
三, 深入瞭解string類
對string有一定了解以後,我們可以來了解C++標準庫定義的一系列豐富的字符串操作,當然都是基於string類型。從某一種程度上來說,string就是一種字符容器。
標準庫爲string定義了很多方法,包括構造、插入(insert)、替換(assign和replace)、比較(compare)、查找(find)、刪除(erase)、連接(append)以及對子串的操作(substr)。而每一種操作都有很多種重載。
比如插入,除了包括標準容器的插入方式以外,string類本身還有一些特有的插入方法。
//example 5:
//與標準容器相同的插入操作:
str.insert(iter, value) //在迭代器iter之前插入value, 返回新元素的迭代器
str.insert(iter, n, value); //在迭代器iter之前插入n個value,返回void
str.insert(iter, begin, end); //在迭代器iter之前插入迭代器begin和end標記範圍內的元素,返回void
//string類特有的插入方法:
str.insert(pos, n, ch); //在下標爲pos的字符之前插入n個字符ch
str.insert(pos, str2); //在下標爲pos的字符之前插入string類型的對象str2的副本
str.insert(pos1, str2, pos2, len); //在下標爲pos1的z字符之前插入string類型str2中從下表爲pos2開始的len個字符
str.insert(pos, cp); //在下標爲pos的字符前插入字符指針cp指向的C風格字符串的副本
總之string是一種非常靈活的字符串類型,標準庫讓我們可以忽略內存管理和具體實現方式,我們只需要關注其接口就好。並且初學者在使用字符串的時候也應儘量使用這種類型,而不是C風格的字符串。
之所以拋棄char*的字符串而選用C++標準程序庫中的string類,是因爲他和前者比較起來,不必擔心內存是否足夠、字符串長度等等,而且作 爲一個類出現,他集成的操作函數足以完成我們大多數情況下(甚至是100%)的需要。我們可以用 = 進行賦值操作,== 進行比較,+ 做串聯(是不是很簡單?)。我們儘可以把它看成是C++的基本數據類型。
好了,進入正題………
首先,爲了在我們的程序中使用string類型,我們必須包含頭文件 。如下:
#include //注意這裏不是string.h string.h是C字符串頭文件
1.聲明一個C++字符串
聲明一個字符串變量很簡單:
string Str;
這樣我們就聲明瞭一個字符串變量,但既然是一個類,就有構造函數和析構函數。上面的聲明沒有傳入參數,所以就直接使用了string的默認的構造函數,這個函數所作的就是把Str初始化爲一個空字符串。String類的構造函數和析構函數如下:
a) string s; //生成一個空字符串s
b) string s(str) //拷貝構造函數 生成str的複製品
c) string s(str,stridx) //將字符串str內“始於位置stridx”的部分當作字符串的初值
d) string s(str,stridx,strlen) //將字符串str內“始於stridx且長度頂多strlen”的部分作爲字符串的初值
e) string s(cstr) //將C字符串作爲s的初值
f) string s(chars,chars_len) //將C字符串前chars_len個字符作爲字符串s的初值。
g) string s(num,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() //刪除字符
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() //返回配置器
下面詳細介紹:
2.1 C++字符串和C字符串的轉換
C++提供的由C++字符串得到對應的C_string的方法是使用data()、c_str()和copy(),其中,data()以字符數組的形式 返回字符串內容,但並不添加’\0’。c_str()返回一個以‘\0’結尾的字符數組,而copy()則把字符串的內容複製或寫入既有的 c_string或字符數組內。C++字符串並不以’\0’結尾。我的建議是在程序中能使用C++字符串就使用,除非萬不得已不選用c_string。由 於只是簡單介紹,詳細介紹掠過,誰想進一步瞭解使用中的注意事項可以給我留言(到我的收件箱)。我詳細解釋。
2.2 大小和容量函數
一個C++字符串存在三種大小:a)現有的字符數,函數是size()和length(),他們等效。Empty()用來檢查字符串是否爲空。 b)max_size() 這個大小是指當前C++字符串最多能包含的字符數,很可能和機器本身的限制或者字符串所在位置連續內存的大小有關係。我們一般情況下不用關心他,應該大小 足夠我們用的。但是不夠用的話,會拋出length_error異常c)capacity()重新分配內存之前 string所能包含的最大字符數。這裏另一個需要指出的是reserve()函數,這個函數爲string重新分配內存。重新分配的大小由其參數決定,
默認參數爲0,這時候會對string進行非強制性縮減。
還有必要再重複一下C++字符串和C字符串轉換的問題,許多人會遇到這樣的問 題,自己做的程序要調用別人的函數、類什麼的(比如數據庫連接函數Connect(char*,char*)),但別人的函數參數用的是char*形式 的,而我們知道,c_str()、data()返回的字符數組由該字符串擁有,所以是一種const char*,要想作爲上面提及的函數的參數,還必須拷貝到一個char*,而我們的原則是能不使用C字符串就不使用。那麼,這時候我們的處理方式是:如果 此函數對參數(也就是char*)的內容不修改的話,我們可以這樣Connect((char*)UserID.c_str(),
(char*)PassWD.c_str()),但是這時候是存在危險的,因爲這樣轉換後的字符串其實是可以修改的(有興趣地可以自己試一試),所以我強 調除非函數調用的時候不對參數進行修改,否則必須拷貝到一個char*上去。當然,更穩妥的辦法是無論什麼情況都拷貝到一個char*上去。同時我們也祈 禱現在仍然使用C字符串進行編程的高手們(說他們是高手一點兒也不爲過,也許在我們還穿開襠褲的時候他們就開始編程了,哈哈…)寫的函數都比較規範,那樣 我們就不必進行強制轉換了。
2.3元素存取
我們可以使用下標操作符[]和函數at()對元素包含的字符進行訪問。但是應該注意的是操作符[]並不檢查索引是否有效(有效索引 0~str.length()),如果索引失效,會引起未定義的行爲。而at()會檢查,如果使用at()的時候索引無效,會拋出 out_of_range異常。
有一個例外不得不說,const string a;的操作符[]對索引值是a.length()仍然有效,其返回值是’\0’。其他的各種情況,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()] //返回 ‘\0’
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立即失效。避免的方法就是不使用。
2.4比較函數
C++字符串支持常見的比較操作符(>,>=,<,<=,==,!=),甚至支持string與C-string的比較(如 str<”hello”)。在使用>,>=,<,<=這些操作符的時候是根據“當前字符特性”將字符按字典順序進行逐一得 比較。字典排序靠前的字符小,比較的順序是從前向後比較,遇到不相等的字符就按這個位置上的兩個字符的比較結果確定兩個字符串的大小。同 時,string(“aaaa”) 另一個功能強大的比較函數是成員函數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’ ‘\0’賦給字符串
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’);
下面我們首先從一些示例開始學習下string類的使用.
1)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s("hehe");
cout<<s<<endl;
cin.get();
}
2)
#include <string>
#include <iostream>
using namespace std;
void main()
{
char chs[] = "hehe";
string s(chs);
cout<<s<<endl;
cin.get();
}
3)
#include <string>
#include <iostream>
using namespace std;
void main()
{
char chs[] = "hehe";
string s(chs,1,3); //指定從chs的索引1開始,最後複製3個字節
cout<<s<<endl;
cin.get();
}
4)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1("hehe");
string s2(s1);
cout<<s2<<endl;
cin.get();
}
5)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1("hehe",2,3);
string s2(s1);
cout<<s2<<endl;
cin.get();
}
6)
#include <string>
#include <iostream>
using namespace std;
void main()
{
char chs[] = "hehe";
string s(chs,3); //將chs前3個字符作爲初值構造
cout<<s<<endl;
cin.get();
}
7)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s(10,'k'); //分配10個字符,初值都是'k'
cout<<s<<endl;
cin.get();
}
//以上是string類實例的構造手段,都很簡單.
9)
//賦新值
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s(10,'k'); //分配10個字符,初值都是'k'
cout<<s<<endl;
s = "hehehehe";
cout<<s<<endl;
s.assign("kdje");
cout<<s<<endl;
s.assign("fkdhfkdfd",5); //重新分配指定字符串的前5的元素內容
cout<<s<<endl;
cin.get();
}
10)
//swap方法交換
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1 = "hehe";
string s2 = "gagaga";
cout<<"s1 : "<<s1<<endl;
cout<<"s2 : "<<s2<<endl;
s1.swap(s2);
cout<<"s1 : "<<s1<<endl;
cout<<"s2 : "<<s2<<endl;
cin.get();
}
11)
//+=,append(),push_back()在尾部添加字符
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "hehe";
s += "gaga";
cout<<s<<endl;
s.append("嘿嘿"); //append()方法可以添加字符串
cout<<s<<endl;
s.push_back('k'); //push_back()方法只能添加一個字符...
cout<<s<<endl;
cin.get();
}
12)
//insert() 插入字符.其實,insert運用好,與其他的插入操作是一樣的.
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "hehe";
s.insert(0,"頭部"); //在頭部插入
s.insert(s.size(),"尾部"); //在尾部插入
s.insert(s.size()/2,"中間");//在中間插入
cout<<s<<endl;
cin.get();
}
13)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
s.erase(0,1); //從索引0到索引1,即刪除掉了'a'
cout<<s<<endl;
//其實,還可以使用replace方法來執行刪除操作
s.replace(2,3,"");//即將指定範圍內的字符替換成"",即變相刪除了
cout<<s<<endl;
cin.get();
}
14)
//clear() 刪除全部字符
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
cout<<s.length()<<endl;
s.clear();
cout<<s.length()<<endl;
//使用earse方法變相全刪除
s = "dkjfd";
cout<<s.length()<<endl;
s.erase(0,s.length());
cout<<s.length()<<endl;
cin.get();
}
15)
//replace() 替換字符
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
s.replace(2,3,"!!!!!");//從索引2開始3個字節的字符全替換成"!!!!!"
cout<<s<<endl;
cin.get();
}
16)
//==,!=,<,<=,>,>=,compare() 比較字符串
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1 = "abcdefg";
string s2 = "abcdefg";
if (s1==s2)cout<<"s1 == s2"<<endl;
else cout<<"s1 != s2"<<endl;
if (s1!=s2)cout<<"s1 != s2"<<endl;
else cout<<"s1 == s2"<<endl;
if (s1>s2)cout<<"s1 > s2"<<endl;
else cout<<"s1 <= s2"<<endl;
if (s1<=s2)cout<<"s1 <= s2"<<endl;
else cout<<"s1 > s2"<<endl;
cin.get();
}
17)
//size(),length() 返回字符數量
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
cout<<s.size()<<endl;
cout<<s.length()<<endl;
cin.get();
}
18)
//max_size() 返回字符的可能最大個數
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
cout<<s.max_size()<<endl;
cin.get();
}
19)
//empty() 判斷字符串是否爲空
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s ;
if (s.empty())
cout<<"s 爲空."<<endl;
else
cout<<"s 不爲空."<<endl;
s = s + "abcdefg";
if (s.empty())
cout<<"s 爲空."<<endl;
else
cout<<"s 不爲空."<<endl;
cin.get();
}
20)
// [ ], at() 存取單一字符
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
cout<<"use []:"<<endl;
for(int i=0; i<s.length(); i++)
{
cout<<s[i]<<endl;
}
cout<<endl;
cout<<"use at():"<<endl;
for(int i=0; i<s.length(); i++)
{
cout<<s.at(i)<<endl;
}
cout<<endl;
cin.get();
}
21)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
const char * chs1 = s.c_str();
const char * chs2 = s.data();
cout<<"use at():"<<endl;
int i;
for(i=0; i<s.length(); i++)
{
cout<<"c_str() : "<<chs1[i]<<endl;
cout<<"data() : "<<chs2[i]<<endl;
}
cout<<"c_str() : "<<chs1<<endl;
cout<<"data() : "<<chs2<<endl;
cout<<endl;
cin.get();
}
22)
// substr() 返回某個子字符串
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
string str = s.substr(5,3);//從索引5開始3個字節
cout<<str<<endl;
cin.get();
}
23)
// find 查找函數
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
string pattern = "fg";
string::size_type pos;
pos = s.find(pattern,0); //從索引0開始,查找符合字符串"f"的頭索引
cout<<pos<<endl;
string str = s.substr(pos,pattern.size());
cout<<str<<endl;
cin.get();
}
24)
// begin() end() 提供類似STL的迭代器支持
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
for(string::iterator iter = s.begin(); iter!=s.end(); iter++)
{
cout<<*iter<<endl;
}
cout<<endl;
cin.get();
}
一個C++字符串存在三種大小:a)現有的字符數,函數是size()和length(),他們等效。 Empty()用來檢查字符串是否爲空。b)max_size() 這個大小是指當前C++字符串最多能包含的字符數,很可能和機器本身的限制或者字符串所在位置連續內存的大小有關係。我們一般情況下不用關心他,應該大小 足夠我們用的。但是不夠用的話,會拋出length_error異常c)capacity()重新分配內存之前 string所能包含的最大字符數。這裏另一個需要指出的是reserve()函數,這個函數爲string重新分配內存。重新分配的大小由其參數決定, 默認參數爲0,這時候會對string進行非強制性縮減
string(const char *s); //用c字符串s初始化
string(int n,char c); //用n個字符c初始化
此外,string類還支持默認構造函數和複製構造函數,如string s1;string s2="hello";都是正確的寫法。當構造的string太長而無法表達時會拋出length_error異常
string類的字符操作:
const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
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的特性描述:
int capacity()const; //返回當前容量(即string中不必增加內存即可存放的元素個數)
int max_size()const; //返回string對象中可存放的最大字符串的長度
int size()const; //返回當前字符串的大小
int length()const; //返回當前字符串的長度
bool empty()const; //當前字符串是否爲空
void resize(int len,char c);//把字符串當前大小置爲len,並用字符c填充不足的部分string類的輸入輸出操作:
string類重載運算符operator>> //用於輸入,同樣重載運算符operator<<用於輸出操作。
函數getline(istream &in,string &s);//用於從輸入流in中讀取字符串到s中,以換行符'\n'分開。
string的賦值:
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迭代器之間的部分賦給字符串
string的連接:
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之間的部分連接到當前字符串的結尾
string的比較:
bool perator==(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
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, 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相似,只不過是從後向前查找
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_iteratorlast);//把[first0,last0)之間的部分替換成[first,last)之間的字符串string類的插入函:
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
string類的刪除函數
iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字符,返回刪除後迭代器的位置
iterator erase(iterator it);//刪除it指向的字符,返回刪除後迭代器的位置
string &erase(int pos = 0, int n = npos);//刪除pos開始的n個字符,返回修改後的字符串
string類的迭代器處理:
string類提供了向前和向後遍歷的迭代器iterator,迭代器提供了訪問各個字符的語法,類似於指針操作,迭代器不檢查範圍。
用string::iterator或string::const_iterator聲明迭代器變量,const_iterator不允許改變迭代的內容。常用迭代器函數有:
const_iterator begin()const;
iterator begin(); //返回string的起始位置
const_iterator end()const;
iterator end(); //返回string的最後一個字符後面的位置
const_iterator rbegin()const;
iterator rbegin(); //返回string的最後一個字符的位置
const_iterator rend()const;
iterator rend(); //返回string第一個字符位置的前面rbegin和rend用於從後向前的迭代訪問,通過設置迭代器
string::reverse_iterator,string::const_reverse_iterator實現
字符串流處理:
通過定義ostringstream和istringstream變量實現,<sstream>頭文件中
例如:
string input("hello,this is a test");
istringstream is(input);
string s1,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();