<string>頭文件

string構造函數

string s  生成一個空字符串s
string s(str)  拷貝構造函數,生成str對象的複製品
string s(str,stridx)  將字符串str對象內"始於位置stridx"的部分當作字符串的初值
string s(str,stridx,strlen)  將字符串str對象始於stridx且最長爲strlen部分作爲字符串的初值
string s(cstr)  將C字符串作爲s的初值
string s(chars,chars_len)  將C字符串前chars_len個字符作爲字符串s的初值
string s(num,c)  生成一個字符串,包含num個c字符
string s(beg,end)  以區間(迭代器iterator )beg到end(不包含end)內的字符作爲字符串s的初值

string析構函數

s.~string()  銷燬所有字符,釋放內存

賦值操作

重載=操作符

basic_string& operator=(const basic_string& rhs)
basic_string& operator=(const E *s)
basic_string& operator=(E c)

string對象= string對象 / 字符串("gaint") / 字符( ' j ')

assign()函數

s.assign(str)  把str對象賦給s對象
s.assign(str,pos,n)  把string 對象中從(下標)pos的後n個元素賦值給s
s.assign(str,pos,string::npos)  把字符串str從索引值p開始到結尾賦給s
s.assign(char_string)  把字符串賦值給s對象
s.assign(char_string,num)  把字符串前num個字符'賦給s對象
s.assign(first,last)  把(迭代器iterator )first和last之間的部分賦給字符串
s.assign(num,char_x)  把num個字符賦給s對象

交字符對象內容

void swap(basic_string& str)  把s與str對象的內容交換

尾部添加字符

重載+= 操作符

basic_string& operator+=(const basic_string& rhs)
basic_string& operator+=(const E *s)
basic_string& operator+=(E c)

string對象+= string對象 / 字符串("gaint") / 字符( ' j ')

append()函數

s.append(str)  把str對象t添加到s對象末尾
s.append(str,pos,num)  把string對象中從(下標)pos的後num個元素添加到s對象末尾
s.append(char_string)  把字符串添加到s對象末尾
s.append(char_string.num)  把字符串的前num個字符添加到s對象末尾
s.append(first,last)  把(迭代器iterator )first到last中的元素添加到s末尾
s.append(num,char_x)  把num個字符添加到s對象末尾

push_back()函數

s.push_back(char_x)  把單個字符添加到s的尾部,這個函數只能增加單個字符

插入字符

insert()函數

basic_string& insert(size_type p0,const basic_string& str)
basic_string& insert(size_type p0,const basic_string& str, size_type pos, size_type n)
basic_string& insert(size_type p0,const E *s, size_type n)
basic_string& insert(size_type p0, const E *s)
basic_string& insert(size_type p0, size_type n, E c)
iterator insert(iterator it, E c)
void insert(iterator it, size_type n, E c)
void insert(iterator it,const_iterator first, const_iterator last)

刪除字符 erase() 函數

basic_string& erase(size_type p0 = 0, size_type n = npos)
iterator erase(iterator it)
iterator erase(iterator first, iterator last)

轉換爲字符串數組

const E *c_str() const  將內容以C_string返回,一個以'\0'結尾的字符數組

替換字符

s.replace(pos ,num , str )  用str代替s中從pos開始的num個字符
s.replace(pos ,num , chars)  用字符串代替s中從pos開始的num個字符
s.replace(pos1,num1,str,pos2, num2)  用str中從pos2開始的num2個字符,代替s中從pos1開始的 num1個字符
s.replace(pos1,num1,chars, num2 )  用字符串中num2個字符,代替s中從pos1開始的num1個字符
s. replace(pos,num,count,char)  用count個字符,代替s中從 pos開始的num個字符
s.replace(First,Last,str )  用 string 代替s中從(迭代器iterator)First到Last的字符
s.replace(First,Last,chars)  用字符串代替s中從(迭代器iterator)First到Last的字符
s.replace(First0,Last0,chars, num )  用字符串的num個字符,代替s中從(迭代器iterator )First到Last的字符
s.replace(First0,Last0,First,Last )  用(迭代器iterator)First開始到Last的字符,代替s中從(迭代器iterator )First0到Last0的字符
s.replace(First,Last,count,char)  用count個字符,代替s中從(迭代器iterator )First到Last的字符

比較字符串

==
!=
<
<=
>
>=

支持string與c_string的比較(如 str<"hello"),字典排序靠前的字符小,比較的順序是從前向後比較,遇到不相等的字符就按這個位置上的兩個字符的比較結果確定兩個字符串的大小。同時,string ("aaaa") <string(aaaaa),compare()他支持多參數處理,支持用索引值和長度定位子串來進行比較。他返回一個整數來表示比較結果,返回值意義如下:0表相等 大於0表大於 小於0表小於

int compare(const basic_string& str) const;
int compare(size_type p0, size_type n0,const basic_string& str
int compare(size_type p0, size_type n0,const basic_string& str, size_type pos, size_type n)
int compare(size_type p0, size_type n0,const E *s) const
int compare(const E *s) const
int compare(size_type p0, size_type n0,const E *s, size_type pos) const

字符串長度

size_type size() const
size_type length() const

返回字符數量,他們等效

字符串最大個數

size_type max_size() const  返回字符串的可能最大個數,C++字符串最多能包含的字符數,很可能和機器本身的限制或者字符串所在位置連續內存的大小有關係

字符串是否爲空

booempty() const  判斷字符串是否爲空

重新指定字符數組長度

void resize(size_type n, E t = E())  重新指定字符數組長度的長度爲n,元素值爲t

新分配之前字符容量

size_type capacity() const  返回重新分配之前的字符容量

保留一定量內存以容納一定數量的字符

void reserve(size_type n = 0)  保留一定量內存以容納一定數量的字符,這個函數爲string重新分配內存,重新分配的大小由其參數決定,默認參數爲0,這時候會對string進行非強制性縮減

存取單一字符

const_reference at(size_type pos) const
reference at(size_type pos)
const_reference operator[](size_type pos) const
reference operator[](size_type pos)

從stream讀取某值

>>
getline()

從stream讀取某值

將謀值寫入stream

<<  將謀值寫入stream

複製string

size_type copy(E *s,size_type n, size_type pos = 0) const  將某值賦值爲一個C_string,把字符串的內容複製或寫入既有的c_string或字符數組內

返回字符數組形式

const E *data() const  將內容以字符數組形式返回,但並不添加’\0’

返回某個子字符串

basic_string substr(size_type pos = 0, size_type n = npos) const

搜索與查找

這些函數返回符合搜索條件的字符區間內的第一個字符的索引,沒找到目標就返回npos.所有的函數的參數說明如下:第一個參數是被搜尋的對象.第二個參數(可有可無)指出string內的搜尋起點索引,第三個參數(可有可無)指出搜尋的字符個數

size_type find(const basic_string& str,size_type pos = 0) const
size_type find(const E *s, size_type pos, size_type n) const
size_type find(const E *s, size_type pos = 0) const
size_type find(E c, size_type pos = 0) const
size_type find(E c, size_type pos = 0) const
size_type rfind(const basic_string& str,size_type pos = npos) const
size_type rfind(const E *s, size_type pos,size_type n = npos) const
size_type rfind(const E *s, size_type pos = npos) const
size_type rfind(E c, size_type pos = npos) const
size_type find_first_of(const basic_string& str,size_type pos = 0) const
size_type find_first_of(const E *s, size_type pos,size_type n) const
size_type find_first_of(const E *s, size_type pos = 0) const
size_type find_first_of(E c, size_type pos = 0) const
size_type find_last_of(const basic_string& str,size_type pos = npos) const
size_type find_last_of(const E *s, size_type pos,size_type n = npos) const
size_type find_last_of(const E *s, size_type pos = npos) const
size_type find_last_of(E c, size_type pos = npos) const
size_type find_first_not_of(const basic_string& str,size_type pos = 0) const
size_type find_first_not_of(const E *s, size_type pos,size_type n) const
size_type find_first_not_of(const E *s, size_type pos = 0) const
size_type find_first_not_of(E c, size_type pos = 0) const
size_type find_last_not_of(const basic_string& str,size_type pos = npos) const
size_type find_last_not_of(const E *s, size_type pos,size_type n) const
size_type find_last_not_of(E c, size_type pos = npos) const
size_type find_last_not_of(const E *s,size_type pos = npos) const

迭代器

正向迭代器

    • iterator begin()
    • const_iterator begin() const
    • iterator end()
    • const_iterator end() const

返回配置器

    • A get_allocator() const

逆向迭代器

    • reverse_iterator rbegin()
    • const_reverse_iterator rbegin() const
    • reverse_iterator rend()
    • const_reverse_iterator rend() const

npos

string::npos  string::npos的類型是string::size_type,所以,一旦需要把一個索引與npos相比,這個索引值必須是string::size_type類型的.是類的靜態成員,更多的情況下,我們可以直接把函數和npos進行比較(如:if(s.find("jia")== string::npos))

 


注意

  • C++字符串並不以’\0’結尾。我的建議是在程序中能使用C++字符串就使用,除非萬不得已不選用c_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字符串進行編程的高手們寫的函數都比較規範,那樣 我們就不必進行強制轉換了
  • 我們可以使用下標操作符[]和函數at()對元素包含的字符進行訪問。但是應該注意的是操作符[]並不檢查索引是否有效(有效索引0~str.length()),如果索引失效,會引起未定義的行爲。而at()會檢查,如果使用 at()的時候索引無效,會拋出out_of_range異常

..............................................................................................................................................................

  1. const string Cstr("const string");  
  2. string Str("string");  
  3. Str[3]; //ok  
  4. Str.at(3); //ok  
  5. Str[100]; //未定義的行爲  
  6. Str.at(100); //throw out_of_range  
  7. Str[Str.length()] //未定義行爲  
  8. Cstr[Cstr.length()] //返回 ‘\0’  
  9. Str.at(Str.length());//throw out_of_range  
  10. Cstr.at(Cstr.length()) ////throw out_of_range  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章