一起來看看C++STL容器之string類
前言
為什么學(xué)習(xí)string類:
在C語言中,字符串是以'\0'結(jié)尾的集合,為了操作方便,在C標(biāo)準(zhǔn)庫中提供一些str系列的函數(shù)(strstr,strcmp,strcpy……),但是這些庫函數(shù)和字符串時分離的,不太符合oop的思想。稍不留神就會造成越界訪問。
在OJ題中,有關(guān)字符串的題目基本以string的形式出現(xiàn),而且在常規(guī)的工作中,為了簡單,方便,快捷,基本都是使用string類,很少有人會使用C庫中的字符串操作函數(shù)。
1.標(biāo)準(zhǔn)庫中的string類
1.string類是表示字符串的字符種類。
2.該類的接口與常規(guī)容器的接口基本相同,在添加一些專門用來操作string的常規(guī)操作。
3.string的底層是:basic_string模板類的別名。
4.不能操作多字節(jié)或者變長字符的序列。
在使用string的時候,必需包含頭文件#include<string>以及using namespace std;
2.string類的常用接口說明
2.1 string對象的常見構(gòu)造
| string() | 構(gòu)造空的string類對象 |
| string(const char* str) | 以常量字符創(chuàng)為參數(shù)構(gòu)造string類 |
| string(size_t n,char ch) | string對象中包含了n個字符c |
| string(const string& s) | 拷貝構(gòu)造函數(shù) |
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1;
string s2("CSDN");
string s3(4, 'A');
string s4(s2);
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
}
[點擊并拖拽以移動]
?運行結(jié)果:

2.2 string類對象的容量操作
| size | 返回字符串的有效長度 |
| length | 和size一致,推薦使用size |
| capacity | 返回總空間大小 |
| clear | 清空有效字符,但是不對capacity有影響 |
| reserve | 為字符串預(yù)留空間 |
| empty | 判斷字符串是否為空串,返回值為bool |
| resize | 將有效字符的個數(shù)該成n個,多出的空間用字符c填充 |
2.2.1 reserve是如何開辟空間的
void reserve (size_t n=0)
void TestPushBack()
{
string s;
size_t sz = s.capacity();
for (int i = 0; i < 1000; ++i)
{
s.push_back('c');
if (sz != s.capacity())//當(dāng)sz和_capacity不相等的時候,說明reserve已經(jīng)增容了。
{
static int n = 1;
sz = s.capacity();
printf("第%d次開辟空間:_capacity=%d\n", n++, sz);
}
}
}運行結(jié)果:

說明在VS的環(huán)境下,reserve每次開辟的空間是前一次空間的約1.5倍。
2.2.2 clear 和 empty
void clear ()
bool empty() const
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("CSDN");
cout << s1.capacity() << endl;
cout << s1.empty() << endl;
s1.clear();
cout << s1.capacity() << endl;
cout << s1.empty() << endl;
}運行結(jié)果:

說明了clear只會清理有效的字符串,不會對空間的大小有影響,當(dāng)clear后,empty的返回值為0,說明了此時的是s1是空字符串。
2.2.3 resize的用法
void resize(size_t n)
void resize(size_t n,char ch)
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("CSDN");
s1.resize(10, 'A');//輸出的是------>CSDNAAAAAAAA
cout <<s1<< endl;
}2.3 string類對象的訪問以及遍歷操作
| operator[ ] | 返回 pos 位置的字符, const string 類對象調(diào)用 |
| begin+end | begin 獲取一個字符的迭代器 + end 獲取最后一個字符下一個位置的迭代器 (正向迭代器) |
| rbegin+rend | begin 獲取一個字符的迭代器 + end 獲取最后一個字符下一個位置的迭代器 (反向迭代器) |
| 范圍for | C++11支持的更簡潔的遍歷方式 |
const_iterator begin()const
iterator begin()
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello CSDN");
auto it1= s1.begin();//正向迭代器
while (it1 != s1.end())//end指向的是最后一個元素的下一位
{
cout << *it1 << " ";
it1++;
}
cout << endl;
auto it2 = s1.rbegin();//反向迭代器
while (it2 != s1.rend())
{
cout << *it2 << " ";
it2++;
}
}運行結(jié)果:

范圍for的使用
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello CSDN");
for (auto ch :s1)
cout << ch<< " ";
}
本質(zhì)上,范圍for調(diào)用的是迭代器。
2.4 string類對象的修改操作
| push_back | 尾插字符ch |
| append | 尾插字符串str |
| operator+= | 尾插字符ch/尾插字符串 |
| c_str | 返回C格式字符串 |
| find+npos | 從字符串pos位置開始往后找字符c,返回該字符在字符串中的位置,npos是size_t的最大值 |
| rfind | 從字符串pos位置開始往前找字符c,返回該字符在字符串中的位置 |
| substr | 在str中從pos位置開始,截取n個字符,然后將其返回 |
2.4.1 push_back 和 append 以及operator+=
void push_back(char ch)
string& append(const char* str)
string& operator+=(char ch)
string& operator+=(const char* str)
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello ");
s1.push_back('C');
s1.append("SDN");
cout << s1 << endl;
string s2("hello ");
s2 += 'w';
s2 += "orld";
cout << s2 << endl;
}運行結(jié)果:

operator +=可以尾插字符,也可以尾插字符串。實際上,operator +=尾插字符的時候,調(diào)用的是push_back,尾插字符串的時候,調(diào)用的是append。
2.4.2 find 和 rfind 以及 substr
size_t find(char c,size_t pos=0) const
size_t rfind(char c,size_t pos=npos)
string substr(size_t pos=0,size_t len=npos)
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello CSDN");
cout << s1.find('C') << endl;
cout << s1.rfind('C',0) << endl;//從pos=0處,即字符'h'往前找,找不到字符'C'返回的是npos
//npos是size_t中的最大值--->4294967295
cout << s1.rfind('C', 8) << endl;
cout << s1.substr(2, 3) << endl;//從字符串的第二個位置開始,截取len=3個字符
}運行結(jié)果:

2.5 string非成員函數(shù)重載
| operator+ | 盡量少用,因為傳值返回,導(dǎo)致深拷貝效率低 |
| operator<< | 輸出運算符重載 |
| operator>> | 輸出運算符重載 |
| getline | 獲取一行字符串 |
| relational operator | 大小比較 |
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)中定位函數(shù)Index的使用方法
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)中定位函數(shù)Index的使用方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解這部分內(nèi)容,需要的朋友可以參考下2017-10-10

