文章目錄
- C++STL詳解(一)—— sring類
- 一、string的定義方式
- 二、string的插入
- 三、string的拼接
- 四、string的洗掉
- 五、string的查找
- 六、string的比較
- 七、string的替換
- 八、string的交換
- 九、string的大小和容量
- 十、string中元素的訪問
- 十一、string中運算子的使用
- 十二、string中與迭代器相關的函式
- 十三、string與字串之間的轉換
- 十四、string中子字串的提取
- 十五、string中的getline函式
C++STL詳解(一)—— sring類
本次內容大綱:

一、string的定義方式
string類實作了多個建構式的多載,常用的建構式如下:
string(); //構造一個空字串
string(const char* s); //復制s所指的字符序列
string(const char* s, size_t n); //復制s所指字符序列的前n個字符
string(size_t n, char c); //生成n個c字符的字串
string(const string& str); //生成str的復制品
string(const string& str, size_t pos, size_t len = npos); //復制str中從字符位置pos開始并跨越len個字符的部分
使用示例:
string s1; //構造空字串
string s2("hello string"); //復制"hello string"
string s3("hello string", 3); //復制"hello string"的前3個字符
string s4(10, 's'); //生成10個's'字符的字串
string s5(s2); //生成s2的復制品
string s6(s2, 0, 4); //復制s2中從字符位置0開始并跨越4個字符的部分
二、string的插入
1、使用push_back進行尾插
void push_back (char c);
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
s.push_back('C');
s.push_back('S');
s.push_back('D');
s.push_back('N');
cout << s << endl; //CSDN
return 0;
}
2、使用insert插入
string& insert (size_t pos, const string& str);
string& insert (size_t pos, const char* s);
iterator insert (iterator p, char c);
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("C"); //C
//insert(pos, str)在pos位置插入字串str
s.insert(1, "S"); //CS
//insert(pos, string)在pos位置插入string物件
string t("D");
s.insert(2, t); //CSD
//insert(pos, char)在pos位置插入字符char
s.insert(s.end(), 'N'); //CSDN
cout << s << endl; //CSDN
return 0;
}
三、string的拼接
使用append函式完成string的拼接:
string& append (const string& str);
string& append (const char* s);
string& append (size_t n, char c);
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("I");
string s2(" like");
//append(string)完成兩個string物件的拼接
s1.append(s2); //I like
//append(str)完成string物件和字串str的拼接
s1.append(" C++"); //I like C++
//append(n, char)將n個字符char拼接到string物件后面
s1.append(3, '!'); //I like C++!!!
cout << s1 << endl; //I like C++!!!
return 0;
}
四、string的洗掉
1、使用pop_back進行尾刪
void pop_back();
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("C++");
s.pop_back();
s.pop_back();
cout << s << endl; //C
return 0;
}
2、使用erase洗掉
string& erase (size_t pos = 0, size_t len = npos);
iterator erase (iterator p);
iterator erase (iterator first, iterator last);
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("I like C++!!!");
//erase(pos, n)洗掉pos位置開始的n個字符
s.erase(8, 5); //I like C
//erase(pos)洗掉pos位置的字符
s.erase(s.end()-1); //I like
//erase(pos1, pos2)洗掉[pos1pos2)上所有字符
s.erase(s.begin() + 1, s.end()); //I
cout << s << endl; //I
return 0;
}
五、string的查找
1、使用find函式正向搜索第一個匹配項
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (char c, size_t pos = 0) const;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("http://www.cplusplus.com/reference/string/string/find/");
//find(string)正向搜索與string物件所匹配的第一個位置
string s2("www");
size_t pos1 = s1.find(s2);
cout << pos1 << endl; //7
//find(str)正向搜索與字串str所匹配的第一個位置
char str[] = "cplusplus.com";
size_t pos2 = s1.find(str);
cout << pos2 << endl; //11
//find(char)正向搜索與字符char所匹配的第一個位置
size_t pos3 = s1.find(':');
cout << pos3 << endl; //4
return 0;
}
2、使用rfind函式反向搜索第一個匹配項
size_t rfind (const string& str, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos = npos) const;
size_t rfind (char c, size_t pos = npos) const;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("http://www.cplusplus.com/reference/string/string/find/");
//rfind(string)反向搜索與string物件所匹配的第一個位置
string s2("string");
size_t pos1 = s1.rfind(s2);
cout << pos1 << endl; //42
//rfind(str)反向搜索與字串str所匹配的第一個位置
char str[] = "reference";
size_t pos2 = s1.rfind(str);
cout << pos2 << endl; //25
//rfind(char)反向搜索與字符char所匹配的第一個位置
size_t pos3 = s1.rfind('/');
cout << pos3 << endl; //53
return 0;
}
六、string的比較
使用compare函式完成比較:
int compare (const string& str) const;
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;
比較規則:
?1、比較字串中第一個不匹配的字符值較小,或者所有比較字符都匹配,但比較字串較短,則回傳小于0的值,
?2、比較字串中第一個不匹配的字符值較大,或者所有比較字符都匹配,但比較字串較長,則回傳大于0的值,
?3、比較的兩個字串相等,則回傳0,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("hello world");
string s2("hello CSDN");
//"hello world"和"hello CSDN"比較
cout << s1.compare(s2) << endl; //1
//"ell"和"hello CSDN"比較
cout << s1.compare(1, 3, s2) << endl; //-1
//"hello"和"hello"比較
cout << s1.compare(0, 4, s2, 0, 4) << endl; //0
return 0;
}
注意:除了支持string類之間進行比較,compare函式還支持string類和字串進行比較,
七、string的替換
使用replace函式完成string的替換:
string& replace (size_t pos, size_t len, const char* s);
string& replace (size_t pos, size_t len, size_t n, char c);
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("hello world");
//replace(pos, len, str)將pos位置開始的len個字符替換為字串str
s.replace(6, 4, "CSDN"); //hello CSDNd
//replace(pos, len, n, char)將pos位置開始的len個字符替換為n個字符char
s.replace(10, 1, 3, '!'); //hello CSDN!!!
cout << s << endl;
return 0;
}
八、string的交換
使用swap函式完成兩個string類的交換:
void swap (string& x, string& y);
void swap (string& str);
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("hello");
string s2("CSDN");
//使用string類的成員函式swap交換s1和s2
s1.swap(s2);
cout << s1 << endl; //CSDN
cout << s2 << endl; //hello
//使用非成員函式swap交換s1和s2
swap(s1, s2);
cout << s1 << endl; //hello
cout << s2 << endl; //CSDN
return 0;
}
九、string的大小和容量
1、使用size函式或length函式獲取當前有效字符的個數
size_t size() const;
size_t length() const;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
cout << s.size() << endl; //4
cout << s.length() << endl; //4
return 0;
}
2、使用max_size函式獲取string物件對多可包含的字符數
size_t max_size() const;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
cout << s.max_size() << endl; //4294967294
return 0;
}
3、使用capacity函式獲取當前物件所分配的存盤空間的大小
size_t capacity() const;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
cout << s.capacity() << endl; //15
return 0;
}
4、使用resize改變當前物件的有效字符的個數
void resize (size_t n);
void resize (size_t n, char c);
resize規則:
?1、當n大于物件當前的size時,將size擴大到n,擴大的字符為c,若c未給出,則默認為’\0’,
?2、當n小于物件當前的size時,將size縮小到n,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("CSDN");
//resize(n)n大于物件當前的size時,將size擴大到n,擴大的字符默認為'\0'
s1.resize(20);
cout << s1 << endl; //CSDN
cout << s1.size() << endl; //20
cout << s1.capacity() << endl; //31
string s2("CSDN");
//resize(n, char)n大于物件當前的size時,將size擴大到n,擴大的字符為char
s2.resize(20, 'x');
cout << s2 << endl; //CSDNxxxxxxxxxxxxxxxx
cout << s2.size() << endl; //20
cout << s2.capacity() << endl; //31
string s3("CSDN");
//resize(n)n小于物件當前的size時,將size縮小到n
s3.resize(2);
cout << s3 << endl; //CS
cout << s3.size() << endl; //2
cout << s3.capacity() << endl; //15
return 0;
}
注意:若給出的n大于物件當前的capacity,則capacity也會根據自己的增長規則進行擴大,
5、使用reserve改變當前物件的容量大小
void reserve (size_t n = 0);
reserve規則:
?1、當n大于物件當前的capacity時,將capacity擴大到n或大于n,
?2、當n小于物件當前的capacity時,什么也不做,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
cout << s << endl; //CSDN
cout << s.size() << endl; //4
cout << s.capacity() << endl; //15
//reverse(n)當n大于物件當前的capacity時,將當前物件的capacity擴大為n或大于n
s.reserve(20);
cout << s << endl; //CDSN
cout << s.size() << endl; //4
cout << s.capacity() << endl; //31
//reverse(n)當n小于物件當前的capacity時,什么也不做
s.reserve(2);
cout << s << endl; //CDSN
cout << s.size() << endl; //4
cout << s.capacity() << endl; //31
return 0;
}
注意:此函式對字串的size沒有影響,并且無法更改其內容,
6、使用clear洗掉物件的內容,洗掉后物件變為空字串
void clear();
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
//clear()洗掉物件的內容,該物件將變為空字串
s.clear();
cout << s << endl; //空字串
return 0;
}
7、使用empty判斷物件是否為空
bool empty() const;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
cout << s.empty() << endl; //0
//clear()洗掉物件的內容,該物件將變為空字串
s.clear();
cout << s.empty() << endl; //1
return 0;
}
十、string中元素的訪問
1、[ ]+下標
?因為string類對[ ]運算子進行了多載,所以我們可以直接使用[ ]+下標訪問物件中的元素,并且該多載使用的是參考回傳,所以我們可以通過[ ]+下標修改對應位置的元素,
???char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
//[]+下標訪問物件元素
for (size_t i = 0; i < s.size(); i++)
{
cout << s[i];
}
cout << endl;
//[]+下標修改物件元素內容
for (size_t i = 0; i < s.size(); i++)
{
s[i] = 'x';
}
cout << s << endl; //xxxx
return 0;
}
2、使用at訪問物件中的元素
?因為at函式也是使用的參考回傳,所以我們也可以通過at函式修改對應位置的元素,
???char& at (size_t pos);
const char& at (size_t pos) const;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
for (size_t i = 0; i < s.size(); i++)
{
//at(pos)訪問pos位置的元素
cout << s.at(i);
}
cout << endl;
for (size_t i = 0; i < s.size(); i++)
{
//at(pos)訪問pos位置的元素,并對其進行修改
s.at(i) = 'x';
}
cout << s << endl; //xxxx
return 0;
}
3、使用范圍for訪問物件中的元素
?需要特別注意的是:若是需要通過范圍for修改物件的元素,則用于接收元素的變數e的型別必須是參考型別,否則e只是物件元素的拷貝,對e的修改不會影響到物件的元素,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
//使用范圍for訪問物件元素
for (auto e : s)
{
cout << e;
}
cout << endl; //CSDN
//使用范圍for訪問物件元素,并對其進行修改
for (auto& e : s) //需要修改物件的元素,e必須是參考型別
{
e = 'x';
}
cout << s << endl; //xxxx
return 0;
}
4、使用迭代器訪問物件中的元素
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
//使用迭代器訪問物件元素
string::iterator it1 = s.begin();
while (it1 != s.end())
{
cout << *it1;
it1++;
}
cout << endl; //CSDN
//使用迭代器訪問物件元素,并對其進行修改
string::iterator it2 = s.begin();
while (it2 != s.end())
{
*it2 += 1;
it2++;
}
cout << s << endl; //DTEO
return 0;
}
十一、string中運算子的使用
1、operator=
?string類中對=運算子進行了多載,多載后的=運算子支持string類的賦值、字串的賦值以及字符的賦值,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
string s2("CSDN");
//支持string類的賦值
s1 = s2;
cout << s1 << endl; //CSDN
//支持字串的賦值
s1 = "hello";
cout << s1 << endl; //hello
//支持字符的賦值
s1 = 'x';
cout << s1 << endl; //x
return 0;
}
2、operator+=
?string類中對+=運算子進行了多載,多載后的+=運算子支持string類的復合賦值、字串的復合賦值以及字符復合的賦值,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
string s2("hello");
//支持string類的復合賦值
s1 += s2;
cout << s1 << endl; //hello
//支持字串的復合賦值
s1 += " CSDN";
cout << s1 << endl; //hello CSDN
//支持字符的復合賦值
s1 += '!';
cout << s1 << endl; //hello CSDN!
return 0;
}
3、operator+
?string類中對+運算子進行了多載,多載后的+運算子支持以下幾種型別的操作:
?string類 + string類
?string類 + 字串
?字串 + string類
?string類 + 字符
?字符 + string類
它們相加后均回傳一個string類物件,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
string s1("super");
string s2("man");
char str[] = "woman";
char ch = '!';
//string類 + string類
s = s1 + s2;
cout << s << endl; //superman
//string類 + 字串
s = s1 + str;
cout << s << endl; //superwoman
//字串 + string類
s = str + s1;
cout << s << endl; //womansuper
//string類 + 字符
s = s1 + ch;
cout << s << endl; //super!
//字符 + string類
s = ch + s1;
cout << s << endl; //!super
return 0;
}
4、operator>> 和 operator<<
?string類中也對>>和<<運算子進行了多載,這就是為什么我們可以直接使用>>和<<對string類進行輸入和輸出的原因,
istream& operator>> (istream& is, string& str);
ostream& operator<< (ostream& os, const string& str);
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cin >> s; //輸入
cout << s << endl; //輸出
return 0;
}
5、relational operators
?string類中還對一系列關系運算子進行了多載,它們分別是==、!=、<、<=、>、>=,多載后的關系運算子支持string類和string類之間的關系比較、string類和字串之間的關系比較、字串和string類之間的關系比較,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abcd");
string s2("abde");
cout << (s1 > s2) << endl; //0
cout << (s1 < s2) << endl; //1
cout << (s1 == s2) << endl; //0
return 0;
}
注意:這些多載的關系比較運算子所比較的都是對應字符的ASCII碼值,
十二、string中與迭代器相關的函式
1、與正向迭代器相關的函式
begin函式:回傳一個指向字串第一個字符的迭代器,
函式原型:
????iterator begin();
?const_iterator begin() const;
end函式:回傳一個指向字串結束字符的迭代器,即’\0’,
函式原型:
????iterator end();
?const_iterator end() const;
使用示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("hello string");
//正向迭代器
string::iterator it = s.begin();
while (it != s.end())
{
cout << *it;
it++;
}
cout << endl; //hello string
return 0;
}
2、與反向迭代器相關的函式
rbegin函式:回傳指向字串最后一個字符的反向迭代器,
函式原型:
????reverse_iterator rbegin();
?const_reverse_iterator rbegin() const;
rend函式:回傳指向字串第一個字符前面的理論元素的反向迭代器,
函式原型:
????reverse_iterator rend();
?const_reverse_iterator rend() const;
使用示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("hello string");
//反向迭代器
string::reverse_iterator rit = s.rbegin();
while (rit != s.rend())
{
cout << *rit;
rit++;
}
cout << endl; //gnirts olleh
return 0;
}
十三、string與字串之間的轉換
1、將字串轉換為string
?將字串轉換為string很簡單,在前面講string的定義方式時就有說到,
#include <iostream>
#include <string>
using namespace std;
int main()
{
//方式一
string s1("hello world");
//方式二
char str[] = "hello world";
string s2(str);
cout << s1 << endl; //hello world
cout << s2 << endl; //hello world
return 0;
}
2、使用c_str或data將string轉換為字串
const char* c_str() const;
const char* data() const;
區別:
- 在C++98中,c_str()回傳 const char* 型別,回傳的字串會以空字符結尾,
- 在C++98中,data()回傳 const char* 型別,回傳的字串不以空字符結尾,
- 但是在C++11版本中,c_str()與data()用法相同,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("hello world ");
const char* str1 = s.data();
const char* str2 = s.c_str();
cout << str1 << endl;
cout << str2 << endl;
return 0;
}
十四、string中子字串的提取
1、使用substr函式提取string中的子字串
string substr (size_t pos = 0, size_t len = npos) const;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abcdef");
string s2;
//substr(pos, n)提取pos位置開始的n個字符序列作為回傳值
s2 = s1.substr(2, 4);
cout << s2 << endl; //cdef
return 0;
}
2、使用copy函式將string的子字串復制到字符陣列中
size_t copy (char* s, size_t len, size_t pos = 0) const;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("abcdef");
char str[20];
//copy(str, n, pos)復制pos位置開始的n個字符到str字串
size_t length = s.copy(str, 4, 2);
//copy函式不會在復制內容的末尾附加'\0',需要手動加
str[length] = '\0';
cout << str << endl; //dcef
return 0;
}
十五、string中的getline函式
?我們知道,使用>>進行輸入操作時,當>>讀取到空格便會停止讀取,基于此,我們將不能用>>將一串含有空格的字串讀入到string物件中,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cin >> s; //輸入:hello CSDN
cout << s << endl; //輸出:hello
return 0;
}
?這時,我們就需要用getline函式完成一串含有空格的字串的讀取操作了,
用法一:
istream& getline (istream& is, string& str);
?getline函式將從is中提取到的字符存盤到str中,直到讀取到換行符’\n’為止,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s); //輸入:hello CSDN
cout << s << endl; //輸出:hello CSDN
return 0;
}
用法二:
istream& getline (istream& is, string& str, char delim);
?getline函式將從is中提取到的字符存盤到str中,直到讀取到分隔符delim或換行符’\n’為止,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s, 'D'); //輸入:hello CSDN
cout << s << endl; //輸出:hello CS
return 0;
}
獻上完整思維導圖:

感謝閱讀!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/287653.html
標籤:其他
