C++string類

文章目錄
- C++string類
- 一、string基本概念
- 二、string初始化
- (1)建構式
- (2)默認建構式
- (3)復制建構式
- 三、string的輸入輸出
- (1)輸入
- (2)輸出
- 四、string的賦值
- (1)用 = 賦值
- (2)用 assign 成員函式復制
- 五、string的連接
- (1)用 + 、+=運算子連接字串
- (2)用成員函式 append 連接字串
- 六、string元素存取 (訪問)
- (1)下標運算子 [ ]
- (2)成員函式at()
- 七、string大小和容量
- 八、string的比較
- (1)`>` 、 `<` 、 `==` 、 `>=` 、`<=`、 `!=` 等比較運算子
- (2)compare()函式
- 九、string的查找
- (1)find ()函式和 rfind()函式
- (2)find_first_of ()函式和 find_last_of ()函式
- (3)find_first_not_of ()函式和find_last_not_of ()
- (4)查找代碼示例
- 十、string內容的修改和替換
- (1)string類的替換函式
- (2)string類的插入函式
- (3)string類的洗掉函式
- 十一、常用操作
- string的子串
- string的交換
- 十二、string類的迭代器處理
- (1)正向迭代
- (2)反向迭代
- 十三、字串流處理
- (1)字串流處理 - 字串輸入流 istringstream
- (2)字串流處理 - 字串輸出流 istringstream
- 十四、轉換成C語言式char *字串
- (1)成員函式 c_str()
- (2)成員函式data()
- (3)成員函式copy()
一、string基本概念
-
string 類是模板類:
typedef basic_string<char> string; -
字串類模板 (basic string)
標準庫字串功能的基礎是 basic string,該類模板提供了許多成員和函式, 與標準容器 類似,該類模板的宣告如下:template <class Ch, class Tr = char_traits<Ch>, class A = allocator<Ch>> class std::basic_string { public: … }在上述模板宣告中,第一個引數(
class Ch)是說明單個字符(Ch)所屬型別(class);第二個引數 (class Tr = char traits < Ch >) 是特性類別, 用以提供字串類別中的所有字符 核心操作, 該特性類別規定了 “復制字符” 或 “比較字符” 的做法; 如果不指定該特性類別,系統會根據現有的字符型別采用默認的特性類別, 第三個引數帶有默認值(class A = allocator < Ch >), 用以定義字串類別所采用的記憶體模式, 通常設定為 “默認記憶體模型 al- locator”, 該模板及其相關功能都定義在名稱空間std中, 由頭檔案< string >給出, -
string 類的所有成員函式
| 函 數 名 稱 | 效 果 |
|---|---|
| 建構式 | 產生或復制字串 |
| 解構式 | 銷毀字串 |
| = , assign | 賦以新值 |
| Swap | 交換兩個字串的內容 |
| + = , append(), push_back() | 添加字符 |
| insert() | 插入字符 |
| erase() | 洗掉字符 |
| clear() | 移除全部字符 |
| resize() | 改變字符數量 |
| replace() | 替換字符 |
| + | 串聯字串 |
| == ,!= ,<,<=,>,>=,compare() | 比較字串內容 |
| size(), length() | 回傳字符數量 |
| max_size() | 回傳字符的最大可能個數 |
| empty() | 判斷字串是否為空 |
| capacity() | 回傳重新分配之前的字符容量 |
| reserve() | 保留記憶體以存盤一定數量的字符 |
| [], at() | 存取單一字符 |
| >> , getline() | 從 stream 中讀取某值 |
| << | 將值寫入 stream |
| copy() | 將內容復制為一個 C - string |
| c_str() | 將內容以 C - string 形式回傳 |
| data() | 將內容以字符陣列形式回傳 |
| substr() | 回傳子字串 |
| find() | 搜尋某子字串或字符 |
| begin(), end() | 提供正向迭代器支持 |
| rbegin(), rend() | 提供逆向迭代器支持 |
| get_allocator() | 回傳配置器 |
二、string初始化
(1)建構式
-
生成字串 str 的復制品
-
函式說明
string s(str) //str是string型別量 -
函式示例
string str="hello world"; string s(str); cout<<s;輸出:
hello world
-
-
將字串 str 中始于 stridx 的部分作為建構式的初值
-
函式說明
string s(str, stridx) // str是string型別量,stridx是起始位置下標 -
函式示例
string str="hello world"; string s(str, 6); cout<<s;輸出:
world
-
-
將字串 str 中始于 strbegin、長度為 strlen 的部分作為字串初值
-
函式說明
string s(str, strbegin, strlen) //str是字串,strbegin是起始位置下標,strlen是字串長度 -
函式示例
char *str = "hello world"; string s(str, 2, 3); cout << s;輸出:
llo
-
-
以 C_string 型別 cstr 作為字串 s 的初值
-
函式說明
string s(cstr) //cstr是字符陣列或字串常量 -
函式示例
char *str = "hello world"; string s(str); cout << s;輸出:
hello world
-
-
以 C_string 型別 cstr 的前 char_len 個字串作為字串 s 的初值
- 函式說明
string s (cstr, char_len) //cstr是字符陣列或字串常量,char_len是長度 - 函式示例
char str[] = "hello world"; string s(str, 6); cout << s;輸出
hello
- 函式說明
-
生成一個字串, 包含 num 個 c 字符
- 函式說明
string s (num, c) //num是int型變數,c是字符 - 函式示例
string s(3, '6'); cout << s輸出:
666
- 函式說明
-
以區間 [beg, end] 內的字符作為字串 s 的初值
- 函式說明
string s ( beg, end) //beg,end均為迭代器 - 函式示例
string str = "hello world"; string::iterator i=str.begin(); string s(i,i+3); cout << s;輸出:
hel
- 函式說明
(2)默認建構式
-
生成空字串
string s;若不提供分配器,則從默認構造的實體獲得分配器,
(3)復制建構式
當構造的string太長而無法表達時會拋出length_error例外
string s2 = "hello";
三、string的輸入輸出
(1)輸入
-
string 支持流讀取運算子
string類多載運算子
operator>>用于輸入;
cin>>s; 讀入s,遇到空格或回車停止,無論原先s是什么內容都會被新讀入的資料替代string s; getline(cin ,s); -
string 支持getline函式
函式
getline(istream &in,string &s);用于從輸入流in中讀取字串到s中,以換行符’\n’分開,string stringObject; cin >> stringObject;
(2)輸出
多載運算子operator<<用于輸出操作,
cout << s;
四、string的賦值
(1)用 = 賦值
string &operator=(const string &s);把字串s賦給當前字串
string s = "hello"
string s1;
s1 = s;
(2)用 assign 成員函式復制
-
函式示例
-
用 assign 成員函式復制
string s1("cat"), s3; s3.assign(s1); -
用 assign 成員函式部分復制
string s1("catpig"), s3; s3.assign(s1, 1, 3);從s1 中下標為1的字符開始復制3個字符給s3
-
-
函式說明
-
用c型別字串s賦值
string &assign(const char *s); -
用c字串s開始的n個字符賦值
string &assign(const char *s,int n); -
把字串s賦給當前字串
string &assign(const string &s); -
用n個字符c賦值給當前字串
string &assign(int n,char c); -
把字串s中從start開始的n個字符賦給當前字串
string &assign(const string &s,int start,int n); -
把first和last迭代器之間的部分賦給字串
string &assign(const_iterator first,const_itertor last);
-
五、string的連接
(1)用 + 、+=運算子連接字串
-
多載說明
string &operator+=(const string &s);把字串s連接到當前字串的結尾
-
運用舉例:
string s1("good "), s2("morning! "); s1 += s2; cout << s1;輸出:
good morning!
(2)用成員函式 append 連接字串
-
函式示例
string s1("good "), s2("morning! "); s1.append(s2); cout << s1; s2.append(s1, 3, s1.size()); //s1.size(),s1字符數 cout << s2;下標為3開始,s1.size()個字符,如果字串內沒有足夠字符,則復制到字串最后一個字符
-
函式說明
- 把c型別字串s連接到當前字串結尾
string &append(const char *s); - 把c型別字串s的前n個字符連接到當前字串結尾
string &append(const char *s,int n); - 同operator+=()
string &append(const string &s); - 把字串s中從pos開始的n個字符連接到當前字串的結尾
string &append(const string &s,int pos,int n); - 在當前字串結尾添加n個字符c
string &append(int n,char c); - 把迭代器first和last之間的部分連接到當前字串的結尾
string &append(const_iterator first,const_iterator last);
- 把c型別字串s連接到當前字串結尾
六、string元素存取 (訪問)
(1)下標運算子 [ ]
-
運算子說明
下標運算子
[]在使用時不檢查索引的有效性,如果下標超出字符的長度范圍,會導致未定義行為;對于常量字串,使用下標運算子時,字串的最后字符 (即'\0') 是有效的, 對應 string 型別物件 (常量型) 最后一個字符的下標是有效的,呼叫回傳字符'\0', -
運算子示例
string s1("Hello"); s1[3] = 'a';
(2)成員函式at()
-
函式說明
函式
at()在使用時會檢查下標是否有效, 如果給定的下標超出字符的長度范圍, 系統 會拋出out of range例外, -
函式示例
string s1("Hello"); for(int i=0;i<s1.length();i++) cout << s1.at(i) << endl;逐個訪問string物件中的字符
七、string大小和容量
-
size()和 length()
int size()const;int length()const;size()和 length(), 這兩個函式會回傳 string 型別物件中的字符個數,且它們的執行效果相同,
-
max_size()
int max_size()const;max_size()函式回傳 string 型別物件最多包含的字符數, 一旦程式使用長度超過
max_size()的 string 操作, 編譯器會拋出length_error例外, -
capacity()
int capacity()const;該函式回傳在重新分配記憶體之前, string 型別物件所能包含的最大字符數,
-
reserve()
呼叫該函式可以為 string 型別物件重新分配 記憶體, 重新分配的大小由其引數決定, reserve() 的默認引數為 0,
-
empty()
bool empty()const;當前字串是否為空
-
resize
void resize(int len,char c);把字串當前大小置為len,并用字符c填充不足的部分
八、string的比較
(1)> 、 < 、 == 、 >= 、<=、 != 等比較運算子
-
比較兩個字串是否相等
bool operator==(const string &s1,const string &s2)const;運算子">","<",">=","<=","!="均被多載用于字串的比較;
(2)compare()函式
compare函式在>時回傳1,<時回傳-1,==時回傳0
-
比較當前字串和s的大小
int compare(const string &s) const; -
比較當前字串從pos開始的n個字符組成的字串與s的大小
int compare(int pos, int n,const string &s)const; -
比較當前字串從pos開始的n個字符組成的字串與s中pos2開始的n2個字符組成的字串的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const; -
與字符陣列的比較
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;
九、string的查找
(1)find ()函式和 rfind()函式
-
find()
-
從pos開始查找字符c在當前字串的位置
int find(char c, int pos = 0) const; -
從pos開始查找字串s在當前串中的位置
int find(const char *s, int pos = 0) const; -
從pos開始查找字串s中前n個字符在當前串中的位置
int find(const char *s, int pos, int n) const; -
從pos開始查找字串s在當前串中的位置
int find(const string &s, int pos = 0) const;
-
-
rfind()
-
回傳最后一個與str中的某個字符匹配的字符,從index開始查找,如果沒找到就回傳string::npos
size_type rfind( const basic_string &str, size_type index );size_type rfind( const char *str, size_type index ); -
回傳最后一個與str中的某個字符匹配的字符,從index開始查找,最多查找num個字符,如果沒找到就回傳string::npos
size_type rfind( const char *str, size_type index, size_type num ); -
回傳最后一個與ch匹配的字符,從index開始查找,如果沒找到就回傳string::npos
size_type rfind( char ch, size_type index );
-
(2)find_first_of ()函式和 find_last_of ()函式
-
find_first_of ()
-
查找在字串中第一個與str中的某個字符匹配的字符,回傳它的位置,搜索從index開始,如果沒找到就回傳string::npos
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 ); -
查找在字串中第一個與str中的某個字符匹配的字符,回傳它的位置,搜索從index開始,最多搜索num個字符,如果沒找到就回傳string::npos
size_type find_first_of( const char *str, size_type index, size_type num ); -
查找在字串中第一個與ch匹配的字符,回傳它的位置,搜索從index開始
size_type find_first_of( char ch, size_type index = 0 );
-
-
find_last_of ()
-
在字串中查找最后一個與str中的某個字符匹配的字符,回傳它的位置,搜索從index開始,如果沒找到就回傳string::nops
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 ); -
在字串中查找最后一個與str中的某個字符匹配的字符,回傳它的位置,搜索從index開始,最多搜索num個字符,如果沒找到就回傳string::nops
size_type find_last_of( const char *str, size_type index, size_type num ); -
在字串中查找最后一個與ch匹配的字符,回傳它的位置,搜索從index開始,如果沒找到就回傳string::nops
size_type find_last_of( char ch, size_type index = npos );
-
(3)find_first_not_of ()函式和find_last_not_of ()
-
find_first_not_of ()
-
在字串中查找第一個與str中的字符都不匹配的字符,回傳它的位置,搜索從index開始,如果沒找到就回傳string::nops
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 ); -
在字串中查找第一個與str中的字符都不匹配的字符,回傳它的位置,搜索從index開始,最多查找num個字符,如果沒找到就回傳string::nops
size_type find_first_not_of( const char *str, size_type index, size_type num ); -
在字串中查找第一個與ch不匹配的字符,回傳它的位置,搜索從index開始,如果沒找到就回傳string::nops
size_type find_first_not_of( char ch, size_type index = 0 );
-
-
find_last_not_of ()
-
在字串中查找最后一個與str中的字符都不匹配的字符,回傳它的位置,搜索從index開始,如果沒找到就回傳string::nops
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); -
在字串中查找最后一個與str中的字符都不匹配的字符,回傳它的位置,搜索從index開始,最多查找num個字符如果沒找到就回傳string::nops
size_type find_last_not_of( const char *str, size_type index, size_type num ); -
在字串中查找最后一個與ch不匹配的字符,回傳它的位置,搜索從index開始,如果沒找到就回傳string::nops
size_type find_last_not_of( char ch, size_type index = npos );
-
(4)查找代碼示例
string s1("hello worlld");
cout << s1.find("ll") << endl;
cout << s1.find("abc") << endl;
cout << s1.rfind("ll") << endl;
cout << s1.rfind("abc") << endl;
cout << s1.find_first_of("abcde") << endl;
cout << s1.find_first_of("abc") << endl;
cout << s1.find_last_of("abcde") << endl;
cout << s1.find_last_of("abc") << endl;
cout << s1.find_first_not_of("abcde") << endl;
cout << s1.find_first_not_of("hello world") << endl;
cout << s1.find_last_not_of("abcde") << endl;
cout << s1.find_last_not_of("hello world") << endl;
輸出:
2
4294967295
9
4294967295
1
4294967295
11
4294967295
0
4294967295
10
4294967295
十、string內容的修改和替換
(1)string類的替換函式
-
replace()函式說明
-
用str中的num個字符替換本字串中的字符,從index開始
basic_string &replace( size_type index, size_type num, const basic_string &str ); -
用str中的num2個字符(從index2開始)替換本字串中的字符,從index1開始,最多num1個字符
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,size_type num2 ); -
用str中的num個字符(從index開始)替換本字串中的字符
basic_string &replace( size_type index, size_type num, const char *str ); -
用str中的num2個字符(從index2開始)替換本字串中的字符,從index1開始,num1個字符
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 ); -
用num2個ch字符替換本字串中的字符,從index開始
basic_string &replace( size_type index, size_type num1, size_type num2, char ch ); -
用str中的字符替換本字串中的字符,迭代器start和end指示范圍
basic_string &replace( iterator start, iterator end, const basic_string &str );basic_string &replace( iterator start, iterator end, const char *str ); -
用str中的num個字符替換本字串中的內容,迭代器start和end指示范圍
basic_string &replace( iterator start, iterator end, const char *str, size_type num ); -
用num個ch字符替換本字串中的內容,迭代器start和end指示范圍
basic_string &replace( iterator start, iterator end, size_type num, char ch );
-
-
函式示例
string s1("hello world"); s1.replace(2,3, "haha"); cout << s1; //將s1中下標2 開始的3個字符換成“haha”輸出:
hehaha world
(2)string類的插入函式
-
insert()函式說明
-
在迭代器i表示的位置前面插入一個字符ch
iterator insert( iterator i, const char &ch ); -
在字串的位置index插入字串str
basic_string &insert( size_type index, const basic_string &str );basic_string &insert( size_type index, const char *str ); -
在字串的位置index插入字串str的子串(從index2開始,長num個字符)
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num ); -
在字串的位置index插入字串str的num個字符
basic_string &insert( size_type index, const char *str, size_type num ); -
在字串的位置index插入num個字符ch的拷貝
basic_string &insert( size_type index, size_type num, char ch ); -
在迭代器i表示的位置前面插入num個字符ch的拷貝
void insert( iterator i, size_type num, const char &ch ); -
在迭代器i表示的位置前面插入一段字符,從start開始,以end結束
void insert( iterator i, iterator start, iterator end );
-
-
函式示例
string s1("hello world"); string s2("show insert"); s1.insert(5, s2) // 將s2插入s1下標5的位置 cout << s1 << endl; s1.insert(2, s2, 5, 3); //將s2中下標5開始的3個字符插入s1下標2的位置 cout << s1 << endl;輸出:
helloshow insert world
heinslloshow insert world
(3)string類的洗掉函式
-
erase()函式說明
-
洗掉pos指向的字符, 回傳指向下一個字符的迭代器
iterator erase( iterator pos ); -
洗掉從start到end的所有字符, 回傳一個迭代器,指向被洗掉的最后一個字符的下一個位置
iterator erase( iterator start, iterator end ); -
洗掉從index索引開始的num個字符, 回傳*this
basic_string &erase( size_type index = 0, size_type num = npos );
-
-
函式示例
string s1("hello worlld"); s1.erase(5); cout << s1; cout << s1.length(); cout << s1.size(); // 去掉下標 5 及之后的字符輸出:
hello55
十一、常用操作
string的子串
-
substr函式
string substr(int pos = 0,int n = npos) const;回傳pos開始的n個字符組成的字串
-
函式示例
string s1("hello world"), s2; s2 = s1.substr(4,5); // 下標4開始5個字符 cout << s2 << endl;輸出:
o wor
string的交換
-
swap函式
void swap(string &s2);交換當前字串與s2的值
-
函式示例
string s1("hello world"), s2("really"); s1.swap(s2); cout << s1 << endl; cout << s2 << endl;輸出:
really
hello world
十二、string類的迭代器處理
string類提供了向前和向后遍歷的迭代器
iterator,迭代器提供了訪問各個字符的語法,類似于指標操作,迭代器不檢查范圍,
用string::iterator或string::const_iterator宣告迭代器變數,const_iterator不允許改變迭代的內容,
(1)正向迭代
-
begin()
onst_iterator begin()const; iterator begin(); //回傳string的起始位置 -
end()
const_iterator end()const; iterator end(); //回傳string的最后一個字符后面的位置
(2)反向迭代
(反向迭代器自增后指向前一個值)
-
rbegin()
const_iterator rbegin()const; iterator rbegin(); //回傳string的最后一個字符的位置 -
rend()
const_iterator rend()const; iterator rend(); //回傳string第一個字符位置的前面
十三、字串流處理
除了標準流和檔案流輸入輸出外,還可以從string進行 輸入輸出;
類似 istream和osteram進行標準流輸入輸出,我們用 istringstream 和 ostringstream進行字串上的輸入 輸出,也稱為記憶體輸入輸出,
頭檔案:#include <sstream>
(1)字串流處理 - 字串輸入流 istringstream
string input("Input test 123 4.7 A");
istringstream inputString(input);
string string1, string2;
int i;
double d;
char c;
inputString >> string1 >> string2 >> i >> d >> c;
cout << string1 << endl << string2 << endl;
cout << i << endl << d << endl << c <<endl;
long L;
if(inputString >> L) cout << "long\n";
else cout << "empty\n";
輸出: Input test 123
4.7 A empty
(2)字串流處理 - 字串輸出流 istringstream
ostringstream outputString;
int a = 10;
outputString << "This " << a << "ok" << endl;
cout << outputString.str();
輸出:
This 10ok
十四、轉換成C語言式char *字串
(1)成員函式 c_str()
-
函式說明
const char *c_str();c_str()函式回傳一個指向正規C字串的指標, 內容與本字串相同.
-
函式示例
string s1("hello world"); printf("%s\n", s1.c_str());輸出:
hello world
(2)成員函式data()
-
函式說明
const char *data();data()函式回傳指向自己的第一個字符的指標.
-
函式示例
string s1("hello world"); const char *p1 = s1.data(); for (int i = 0; i < s1.length(); i++) printf("%c", *(p1 + i)); //s1.data() 回傳一個char * 型別的字串,對s1 的修改可能會使p1出錯輸出:
hello world
(3)成員函式copy()
-
函式說明
size_type copy( char *str, size_type num, size_type index );copy()函式拷貝自己的num個字符到str中(從索引index開始),回傳值是拷貝的字符數
-
函式示例
string s1("hello world"); int len = s1.length(); char *p2 = new char[len + 1]; s1.copy(p2, 5, 0); p2[5] = 0; cout << p2 << endl; // s1.copy(p2,5,0) 從s1的下標0的字符開始制作一個最長5個字符長度的字串副本并將其賦值給p2,回傳值表明實際復制字串的長度,輸出:
hello
【知識索引】【C++入門】
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/259774.html
標籤:其他
上一篇:Codeforces Round #701 (Div. 2) A ~ F ,6題全,超高質量良心題解【每日億題】2021/2/13
下一篇:Android——Lambda
