C++ std::string使用總結

構造函數(Constructors)

語法:

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的元素爲初值

添加文本(append)

語法:

basic_string &append( const basic_string &str );
basic_string &append( const char *str );
basic_string &append( const basic_string &str, size_type index, size_type len );
basic_string &append( const char *str, size_type num );
basic_string &append( size_type num, char ch );
basic_string &append( input_iterator start, input_iterator end );

append() 函數可以完成以下工作:

  • 在字符串的末尾添加str,
  • 在字符串的末尾添加str的子串,子串以index索引開始,長度爲len
  • 在字符串的末尾添加str中的num個字符,
  • 在字符串的末尾添加num個字符ch,
  • 在字符串的末尾添加以迭代器start和end表示的字符序列.

賦值(assign)

語法:

basic_string &assign( const basic_string &str );
basic_string &assign( const char *str );
basic_string &assign( const char *str, size_type num );
basic_string &assign( const basic_string &str, size_type index, size_type len );
basic_string &assign( size_type num, char ch );

函數以下列方式賦值:

  • 用str爲字符串賦值,
  • 用str的開始num個字符爲字符串賦值,
  • 用str的子串爲字符串賦值,子串以index索引開始,長度爲len
  • 用num個字符ch爲字符串賦值.

at

語法:

reference at( size_type index );

at()函數返回一個引用,指向在index位置的字符. 如果index不在字符串範圍內, at() 將報告"out of range"錯誤,並拋出out_of_range異常。

begin

語法:

iterator begin();

begin()函數返回一個迭代器,指向字符串的第一個元素。

c_str

語法:

const char *c_str();

c_str()函數返回一個指向正規C字符串的指針, 內容與本字符串相同.

容量(capacity)

語法:

size_type capacity();

capacity()函數返回在重新申請更多的空間前字符串可以容納的字符數. 這個數字至少與 size()一樣大.

比較(compare)

語法:

int compare( const basic_string &str );
int compare( const char *str );
int compare( size_type index, size_type length, const basic_string &str );
int compare( size_type index, size_type length, const basic_string &str, size_type index2,
size_type length2 );
int compare( size_type index, size_type length, const char *str, size_type length2 );

compare()函數以多種方式比較本字符串和str,返回:
返回值情況:
小於零 this < str
零 this == str
大於零 this > str

  • 比較自己和str,
  • 比較自己的子串和str,子串以index索引開始,長度爲length
  • 比較自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
  • 比較自己的子串和str的子串,其中str的子串以索引0開始,長度爲length2,自己的子串以index開始,長度爲length

拷貝(copy)

語法:

size_type copy( char *str, size_type num, size_type index );

copy()函數拷貝自己的num個字符到str中(從索引index開始)。返回值是拷貝的字符數

data

語法:

const char *data();

data()函數返回指向自己的第一個字符的指針.

empty

語法:

bool empty();

如果字符串爲空則empty()返回真(true),否則返回假(false).

end

語法:

iterator end();

end()函數返回一個迭代器,指向字符串的末尾(最後一個字符的下一個位置).

刪除(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.

參數index num 有默認值, 這意味着erase()可以這樣調用:只帶有index以刪除index後的所有字符,或者不帶有任何參數以刪除所有字符.

查找(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

find_first_of

語法:

size_type find_first_of( const basic_string &str, size_type index = 0 );
size_type find_first_of( const char *str, size_type index = 0 );
size_type find_first_of( const char *str, size_type index, size_type num );
size_type find_first_of( char ch, size_type index = 0 );

find_first_of()函數:

  • 查找在字符串中第一個與str中的某個字符匹配的字符,返回它的位置。搜索從index開始,如果沒找到就返回string::npos
  • 查找在字符串中第一個與str中的某個字符匹配的字符,返回它的位置。搜索從index開始,最多搜索num個字符。如果沒找到就返回string::npos,
  • 查找在字符串中第一個與ch匹配的字符,返回它的位置。搜索從index開始。

find_first_not_of

語法:

size_type find_first_not_of( const basic_string &str, size_type index = 0 );
size_type find_first_not_of( const char *str, size_type index = 0 );
size_type find_first_not_of( const char *str, size_type index, size_type num );
size_type find_first_not_of( char ch, size_type index = 0 );

find_first_not_of()函數:

  • 在字符串中查找第一個與str中的字符都不匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops
  • 在字符串中查找第一個與str中的字符都不匹配的字符,返回它的位置。搜索從index開始,最多查找num個字符。如果沒找到就返回string::nops
  • 在字符串中查找第一個與ch不匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops

find_last_of

語法:

size_type find_last_of( const basic_string &str, size_type index = npos );
size_type find_last_of( const char *str, size_type index = npos );
size_type find_last_of( const char *str, size_type index, size_type num );
size_type find_last_of( char ch, size_type index = npos );

find_last_of()函數:

  • 在字符串中查找最後一個與str中的某個字符匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops
  • 在字符串中查找最後一個與str中的某個字符匹配的字符,返回它的位置。搜索從index開始,最多搜索num個字符。如果沒找到就返回string::nops
  • 在字符串中查找最後一個與ch匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops

find_last_not_of

語法:

size_type find_last_not_of( const basic_string &str, size_type index = npos );
size_type find_last_not_of( const char *str, size_type index = npos);
size_type find_last_not_of( const char *str, size_type index, size_type num );
size_type find_last_not_of( char ch, size_type index = npos );

find_last_not_of()函數:

  • 在字符串中查找最後一個與str中的字符都不匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops
  • 在字符串中查找最後一個與str中的字符都不匹配的字符,返回它的位置。搜索從index開始,最多查找num個字符如果沒找到就返回string::nops
  • 在字符串中查找最後一個與ch不匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops

get_allocator

語法:

allocator_type get_allocator();

get_allocator()函數返回本字符串的配置器.

插入(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結束.

長度(length)

語法:

size_type length();

length()函數返回字符串的長度. 這個數字應該和size()返回的數字相同.

max_size
語法:

size_type max_size();

max_size()函數返回字符串能保存的最大字符數。

rbegin

語法:

const reverse_iterator rbegin();

rbegin()返回一個逆向迭代器,指向字符串的最後一個字符。

rend

語法:

const reverse_iterator rend();

rend()函數返回一個逆向迭代器,指向字符串的開頭(第一個字符的前一個位置)。

替換(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指示範圍.

保留空間(reserve)

語法:

void reserve( size_type num );

reserve()函數設置本字符串的capacity 以保留num個字符空間。

resize

語法:

void resize( size_type num );
void resize( size_type num, char ch );

resize()函數改變本字符串的大小到num, 新空間的內容不確定。也可以指定用ch填充。

rfind

語法:

size_type rfind( const basic_string &str, size_type index );
size_type rfind( const char *str, size_type index );
size_type rfind( const char *str, size_type index, size_type num );
size_type rfind( char ch, size_type index );

rfind()函數:

  • 返回最後一個與str中的某個字符匹配的字符,從index開始查找。如果沒找到就返回string::npos
  • 返回最後一個與str中的某個字符匹配的字符,從index開始查找,最多查找num個字符。如果沒找到就返回string::npos
  • 返回最後一個與ch匹配的字符,從index開始查找。如果沒找到就返回string::npos

size

語法:

size_type size();

size()函數返回字符串中現在擁有的字符數。

substr

語法:

basic_string substr( size_type index, size_type num = npos );

substr()返回本字符串的一個子串,從index開始,長num個字符。如果沒有指定,將是默認值 string::npos。這樣,substr()函數將簡單的返回從index開始的剩餘的字符串。

交換(swap)

語法:

void swap( basic_string &str );

swap()函數把str和本字符串交換。

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