【學習筆記】C++中的string類詳解

(一)string類的構造函數:

string(); 

string( size_type length, char ch ); 

string( const char *str ); 

string( const char *str, size_type length ); 

string( string &str, size_type index, size_type length ); 

string( input_iterator start, input_iterator end );

字符串的構造函數創建一個新字符串,包括:

  • 以length爲長度的ch的拷貝(即length個ch)
  • 以str爲初值 (長度任意),
  • 以index爲索引開始的子串,長度爲length, 或者
  • 以從start到end的元素爲初值. 
e.g.:
    string str1( 5, 'c' );
    string str2( "Now is the time..." );
    string str3( str2, 11, 4 );
    cout << str1 << endl;
    cout << str2 << endl;
    cout << str3 << endl;
Output:
    ccccc
    Now is the time...
    time


(二)string類的操作符:
  ==
  >
  <
  >=
  <=
  !=
  +
  +=
  []
==, >, <, >=, <=, !=比較字符串. 
+ , += 操作符連接兩個字符串,
[]獲取特定的字符

(三)string類的迭代器:
begin() 指向字符串的第一個元素,
end() 指向字符串的末尾(最後一個字符的下一個位置).
rbegin() 指向字符串的最後一個字符,(逆向迭代器)
rend() 指向字符串的開頭(第一個字符的前一個位置).


(四)string類的一些常用函數
char* c_str()函數返回一個指向正規C字符串的指針, 內容與本字符串相同.
size_type length()函數返回字符串長度.
bool empty()如果字符串爲空則empty()返回真(true),否則返回假(false).
size_type copy( char *str, size_type num, size_type index )拷貝自己的num個字符到str中(從索引index開始)。返回值是拷貝的字符數
void swap(basic_string &str) 交換字符串內容.

erase()刪除函數:
iterator erase( iterator pos );
iterator erase( iterator start, iterator end );
basic_string &erase( size_type index = 0, size_type num = npos );

erase()函數:

  • 刪除pos指向的字符, 返回指向下一個字符的迭代器,
  • 刪除從start到end的所有字符, 返回一個迭代器,指向被刪除的最後一個字符的下一個位置
  • 刪除從index索引開始的num個字符, 返回*this
find()查找函數:
size_type find( const basic_string &str, size_type index );
size_type find( const char *str, size_type index );
size_type find( const char *str, size_type index, size_type length );
size_type find( char ch, size_type index );

find()函數:

  • 返回str在字符串中第一次出現的位置(從index開始查找)如果沒找到則返回string::npos,
  • 返回str在字符串中第一次出現的位置(從index開始查找,長度爲length)。如果沒找到就返回string::npos,
  • 返回字符ch在字符串中第一次出現的位置(從index開始查找)。如果沒找到就返回string::npos
replace()替代函數:
basic_string &replace( size_type index, size_type num, const basic_string &str );
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,size_type num2 );
basic_string &replace( size_type index, size_type num, const char *str );
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
basic_string &replace( iterator start, iterator end, const basic_string &str );
basic_string &replace( iterator start, iterator end, const char *str );
basic_string &replace( iterator start, iterator end, const char *str, size_type num );
basic_string &replace( iterator start, iterator end, size_type num, char ch );

replace()函數:

  • 用str中的num個字符替換本字符串中的字符,從index開始
  • 用str中的num2個字符(從index2開始)替換本字符串中的字符,從index1開始,最多num1個字符
  • 用str中的num個字符(從index開始)替換本字符串中的字符
  • 用str中的num2個字符(從index2開始)替換本字符串中的字符,從index1開始,num1個字符
  • 用num2個ch字符替換本字符串中的字符,從index開始
  • 用str中的字符替換本字符串中的字符,迭代器start和end指示範圍
  • 用str中的num個字符替換本字符串中的內容,迭代器start和end指示範圍,
  • 用num個ch字符替換本字符串中的內容,迭代器start和end指示範圍. 
insert()插入函數:
iterator insert( iterator i, const char &ch );
basic_string &insert( size_type index, const basic_string &str );
basic_string &insert( size_type index, const char *str );
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
basic_string &insert( size_type index, const char *str, size_type num );
basic_string &insert( size_type index, size_type num, char ch );
void insert( iterator i, size_type num, const char &ch );
void insert( iterator i, iterator start, iterator end );

insert()函數:

  • 在迭代器i表示的位置前面插入一個字符ch,
  • 在字符串的位置index插入字符串str,
  • 在字符串的位置index插入字符串str的子串(從index2開始,長num個字符),
  • 在字符串的位置index插入字符串str的num個字符,
  • 在字符串的位置index插入num個字符ch的拷貝,
  • 在迭代器i表示的位置前面插入num個字符ch的拷貝,
  • 在迭代器i表示的位置前面插入一段字符,從start開始,以end結束. 

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