【C++】 C++字符串类
1. C++ Strings(字符串)
| Constructors | 構(gòu)造函數(shù),用于字符串初始化 |
| Operators | 操作符,用于字符串比較和賦值 |
| append() | 在字符串的末尾添加文本 |
| assign() | 為字符串賦新值 |
| at() | 按給定索引值返回字符 |
| begin() | 返回一個(gè)迭代器,指向第一個(gè)字符 |
| c_str() | 將字符串以C字符數(shù)組的形式返回 |
| capacity() | 返回重新分配空間前的字符容量 |
| compare() | 比較兩個(gè)字符串 |
| copy() | 將內(nèi)容復(fù)制為一個(gè)字符數(shù)組 |
| data() | 返回內(nèi)容的字符數(shù)組形式 |
| empty() | 如果字符串為空,返回真 |
| end() | 返回一個(gè)迭代器,指向字符串的末尾。(最后一個(gè)字符的下一個(gè)位置) |
| erase() | 刪除字符 |
| find() | 在字符串中查找字符 |
| find_first_of() | 查找第一個(gè)與value中的某值相等的字符 |
| find_first_not_of() | 查找第一個(gè)與value中的所有值都不相等的字符 |
| find_last_of() | 查找最后一個(gè)與value中的某值相等的字符 |
| find_last_not_of() | 查找最后一個(gè)與value中的所有值都不相等的字符 |
| get_allocator() | 返回配置器 |
| insert() | 插入字符 |
| length() | 返回字符串的長(zhǎng)度 |
| max_size() | 返回字符的最大可能個(gè)數(shù) |
| rbegin() | 返回一個(gè)逆向迭代器,指向最后一個(gè)字符 |
| rend() | 返回一個(gè)逆向迭代器,指向第一個(gè)元素的前一個(gè)位置 |
| replace() | 替換字符 |
| reserve() | 保留一定容量以容納字符串(設(shè)置capacity值) |
| resize() | 重新設(shè)置字符串的大小 |
| rfind() | 查找最后一個(gè)與value相等的字符(逆向查找) |
| size() | 返回字符串中字符的數(shù)量 |
| substr() | 返回某個(gè)子字符串 |
| swap() | 交換兩個(gè)字符串的內(nèi)容 |
2. C++ Strings(字符串)
構(gòu)造函數(shù)(Constructors)
語(yǔ)法:| 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 ); |
字符串的構(gòu)造函數(shù)創(chuàng)建一個(gè)新字符串,包括:
- 以length為長(zhǎng)度的ch的拷貝(即length個(gè)ch)
- 以str為初值 (長(zhǎng)度任意),
- 以index為索引開(kāi)始的子串,長(zhǎng)度為length, 或者
- 以從start到end的元素為初值.
顯示
cccccNow is the time...time操作符(Operators)
語(yǔ)法:| ==><>=<=!=++=[] |
你可以用 ==, >, <, >=, <=, and !=比較字符串. 可以用 + 或者 += 操作符連接兩個(gè)字符串, 并且可以用[]獲取特定的字符.
相關(guān)主題:at(), compare().
添加文本(append)
語(yǔ)法:| 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() 函數(shù)可以完成以下工作:
- 在字符串的末尾添加str,
- 在字符串的末尾添加str的子串,子串以index索引開(kāi)始,長(zhǎng)度為len
- 在字符串的末尾添加str中的num個(gè)字符,
- 在字符串的末尾添加num個(gè)字符ch,
- 在字符串的末尾添加以迭代器start和end表示的字符序列.
顯示
Hello World!!!!!!!!!! 相關(guān)主題:+ 操作符
賦值(assign)
語(yǔ)法:| 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 ); |
函數(shù)以下列方式賦值:
- 用str為字符串賦值,
- 用str的開(kāi)始num個(gè)字符為字符串賦值,
- 用str的子串為字符串賦值,子串以index索引開(kāi)始,長(zhǎng)度為len
- 用num個(gè)字符ch為字符串賦值.
顯示
andat
語(yǔ)法:| reference at( size_type index ); |
at()函數(shù)返回一個(gè)引用,指向在index位置的字符. 如果index不在字符串范圍內(nèi), at() 將報(bào)告"out of range"錯(cuò)誤,并拋出out_of_range異常。 比如下列代碼:
string text = "ABCDEF";char ch = text.at( 2 );顯示字符 'C'.
相關(guān)主題:[]操作符
begin
語(yǔ)法:| iterator begin(); |
begin()函數(shù)返回一個(gè)迭代器,指向字符串的第一個(gè)元素.
相關(guān)主題:end()
c_str
語(yǔ)法:| const char *c_str(); |
c_str()函數(shù)返回一個(gè)指向正規(guī)C字符串的指針, 內(nèi)容與本字符串相同.
相關(guān)主題:[] 操作符
容量(capacity)
語(yǔ)法:| size_type capacity(); |
capacity()函數(shù)返回在重新申請(qǐng)更多的空間前字符串可以容納的字符數(shù). 這個(gè)數(shù)字至少與 size()一樣大.
相關(guān)主題:max_size(), reserve(), resize(), size(),
比較(compare)
語(yǔ)法:| 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()函數(shù)以多種方式比較本字符串和str,返回:
| 小于零 | this < str |
| 零 | this == str |
| 大于零 | this > str |
不同的函數(shù):
- 比較自己和str,
- 比較自己的子串和str,子串以index索引開(kāi)始,長(zhǎng)度為length
- 比較自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
- 比較自己的子串和str的子串,其中str的子串以索引0開(kāi)始,長(zhǎng)度為length2,自己的子串以index開(kāi)始,長(zhǎng)度為length
操作符
拷貝(copy)
語(yǔ)法:| size_type copy( char *str, size_type num, size_type index ); |
copy()函數(shù)拷貝自己的num個(gè)字符到str中(從索引index開(kāi)始)。返回值是拷貝的字符數(shù)
data
語(yǔ)法:| const char *data(); |
data()函數(shù)返回指向自己的第一個(gè)字符的指針.
相關(guān)主題:c_str()
empty
語(yǔ)法:| bool empty(); |
如果字符串為空則empty()返回真(true),否則返回假(false).
end
語(yǔ)法:| iterator end(); |
end()函數(shù)返回一個(gè)迭代器,指向字符串的末尾(最后一個(gè)字符的下一個(gè)位置).
相關(guān)主題:begin()
刪除(erase)
語(yǔ)法:| iterator erase( iterator pos );iterator erase( iterator start, iterator end );basic_string &erase( size_type index = 0, size_type num = npos ); |
erase()函數(shù)可以:
- 刪除pos指向的字符, 返回指向下一個(gè)字符的迭代器,
- 刪除從start到end的所有字符, 返回一個(gè)迭代器,指向被刪除的最后一個(gè)字符的下一個(gè)位置
- 刪除從index索引開(kāi)始的num個(gè)字符, 返回*this.
將顯示
The original string is 'So, you like donuts, eh? Well, have all the donuts in the world!'Now the string is 'So, you like donuts, eh? Well, have all the donuts'Now the string is 'So, you like donuts, eh?'Now the string is ''查找(find)
語(yǔ)法:| 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()函數(shù):
- 返回str在字符串中第一次出現(xiàn)的位置(從index開(kāi)始查找)。如果沒(méi)找到則返回string::npos,
- 返回str在字符串中第一次出現(xiàn)的位置(從index開(kāi)始查找,長(zhǎng)度為length)。如果沒(méi)找到就返回string::npos,
- 返回字符ch在字符串中第一次出現(xiàn)的位置(從index開(kāi)始查找)。如果沒(méi)找到就返回string::npos
find_first_of
語(yǔ)法:| 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()函數(shù):
- 查找在字符串中第一個(gè)與str中的某個(gè)字符匹配的字符,返回它的位置。搜索從index開(kāi)始,如果沒(méi)找到就返回string::npos
- 查找在字符串中第一個(gè)與str中的某個(gè)字符匹配的字符,返回它的位置。搜索從index開(kāi)始,最多搜索num個(gè)字符。如果沒(méi)找到就返回string::npos,
- 查找在字符串中第一個(gè)與ch匹配的字符,返回它的位置。搜索從index開(kāi)始。
find()
find_first_not_of
語(yǔ)法:| 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()函數(shù):
- 在字符串中查找第一個(gè)與str中的字符都不匹配的字符,返回它的位置。搜索從index開(kāi)始。如果沒(méi)找到就返回string::nops
- 在字符串中查找第一個(gè)與str中的字符都不匹配的字符,返回它的位置。搜索從index開(kāi)始,最多查找num個(gè)字符。如果沒(méi)找到就返回string::nops
- 在字符串中查找第一個(gè)與ch不匹配的字符,返回它的位置。搜索從index開(kāi)始。如果沒(méi)找到就返回string::nops
find()
find_last_of
語(yǔ)法:| 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()函數(shù):
- 在字符串中查找最后一個(gè)與str中的某個(gè)字符匹配的字符,返回它的位置。搜索從index開(kāi)始。如果沒(méi)找到就返回string::nops
- 在字符串中查找最后一個(gè)與str中的某個(gè)字符匹配的字符,返回它的位置。搜索從index開(kāi)始,最多搜索num個(gè)字符。如果沒(méi)找到就返回string::nops
- 在字符串中查找最后一個(gè)與ch匹配的字符,返回它的位置。搜索從index開(kāi)始。如果沒(méi)找到就返回string::nops
find()
find_last_not_of
語(yǔ)法:| 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()函數(shù):
- 在字符串中查找最后一個(gè)與str中的字符都不匹配的字符,返回它的位置。搜索從index開(kāi)始。如果沒(méi)找到就返回string::nops
- 在字符串中查找最后一個(gè)與str中的字符都不匹配的字符,返回它的位置。搜索從index開(kāi)始,最多查找num個(gè)字符如果沒(méi)找到就返回string::nops
- 在字符串中查找最后一個(gè)與ch不匹配的字符,返回它的位置。搜索從index開(kāi)始。如果沒(méi)找到就返回string::nops
find()
get_allocator
語(yǔ)法:| allocator_type get_allocator(); |
get_allocator()函數(shù)返回本字符串的配置器.
插入(insert)
語(yǔ)法:| 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()函數(shù)的功能非常多:
- 在迭代器i表示的位置前面插入一個(gè)字符ch,
- 在字符串的位置index插入字符串str,
- 在字符串的位置index插入字符串str的子串(從index2開(kāi)始,長(zhǎng)num個(gè)字符),
- 在字符串的位置index插入字符串str的num個(gè)字符,
- 在字符串的位置index插入num個(gè)字符ch的拷貝,
- 在迭代器i表示的位置前面插入num個(gè)字符ch的拷貝,
- 在迭代器i表示的位置前面插入一段字符,從start開(kāi)始,以end結(jié)束.
replace()
長(zhǎng)度(length)
語(yǔ)法:| size_type length(); |
length()函數(shù)返回字符串的長(zhǎng)度. 這個(gè)數(shù)字應(yīng)該和size()返回的數(shù)字相同.
相關(guān)主題:size()
max_size
語(yǔ)法:| size_type max_size(); |
max_size()函數(shù)返回字符串能保存的最大字符數(shù)。
rbegin
語(yǔ)法:| const reverse_iterator rbegin(); |
rbegin()返回一個(gè)逆向迭代器,指向字符串的最后一個(gè)字符。
相關(guān)主題:rend()
rend
語(yǔ)法:| const reverse_iterator rend(); |
rend()函數(shù)返回一個(gè)逆向迭代器,指向字符串的開(kāi)頭(第一個(gè)字符的前一個(gè)位置)。
相關(guān)主題:rbegin()
替換(replace)
語(yǔ)法:| 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()函數(shù):
- 用str中的num個(gè)字符替換本字符串中的字符,從index開(kāi)始
- 用str中的num2個(gè)字符(從index2開(kāi)始)替換本字符串中的字符,從index1開(kāi)始,最多num1個(gè)字符
- 用str中的num個(gè)字符(從index開(kāi)始)替換本字符串中的字符
- 用str中的num2個(gè)字符(從index2開(kāi)始)替換本字符串中的字符,從index1開(kāi)始,num1個(gè)字符
- 用num2個(gè)ch字符替換本字符串中的字符,從index開(kāi)始
- 用str中的字符替換本字符串中的字符,迭代器start和end指示范圍
- 用str中的num個(gè)字符替換本字符串中的內(nèi)容,迭代器start和end指示范圍,
- 用num個(gè)ch字符替換本字符串中的內(nèi)容,迭代器start和end指示范圍.
insert()
保留空間(reserve)
語(yǔ)法:| void reserve( size_type num ); |
reserve()函數(shù)設(shè)置本字符串的capacity 以保留num個(gè)字符空間。
相關(guān)主題:capacity()
resize
語(yǔ)法:| void resize( size_type num );void resize( size_type num, char ch ); |
resize()函數(shù)改變本字符串的大小到num, 新空間的內(nèi)容不確定。也可以指定用ch填充。
rfind
語(yǔ)法:| 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()函數(shù):
- 返回最后一個(gè)與str中的某個(gè)字符匹配的字符,從index開(kāi)始查找。如果沒(méi)找到就返回string::npos
- 返回最后一個(gè)與str中的某個(gè)字符匹配的字符,從index開(kāi)始查找,最多查找num個(gè)字符。如果沒(méi)找到就返回string::npos
- 返回最后一個(gè)與ch匹配的字符,從index開(kāi)始查找。如果沒(méi)找到就返回string::npos
find()
size
語(yǔ)法:| size_type size(); |
size()函數(shù)返回字符串中現(xiàn)在擁有的字符數(shù)。
相關(guān)主題:length(), max_size()
substr
語(yǔ)法:| basic_string substr( size_type index, size_type num = npos ); |
substr()返回本字符串的一個(gè)子串,從index開(kāi)始,長(zhǎng)num個(gè)字符。如果沒(méi)有指定,將是默認(rèn)值 string::npos。這樣,substr()函數(shù)將簡(jiǎn)單的返回從index開(kāi)始的剩余的字符串。
例如:
string s("What we have here is a failure to communicate");string sub = s.substr(21);cout << "The original string is " << s << endl;cout << "The substring is " << sub << endl;顯示:
The original string is What we have here is a failure to communicateThe substring is a failure to communicate交換(swap)
語(yǔ)法:| void swap( basic_string &str ); |
swap()函數(shù)把str和本字符串交換。例如:
string first( "This comes first" );string second( "And this is second" );first.swap( second );cout << first << endl;cout << second << endl;顯示:
And this is secondThis comes first總結(jié)
以上是生活随笔為你收集整理的【C++】 C++字符串类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【C++】 C++标准模板库(十二) 迭
- 下一篇: 【C++】 为什么C++空类占一个字节