日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

一起來看看C++STL容器之string類_C 語言

作者:長大Leslie ? 更新時間: 2022-05-06 編程語言

前言

為什么學習string類:

在C語言中,字符串是以'\0'結尾的集合,為了操作方便,在C標準庫中提供一些str系列的函數(strstr,strcmp,strcpy……),但是這些庫函數和字符串時分離的,不太符合oop的思想。稍不留神就會造成越界訪問。

在OJ題中,有關字符串的題目基本以string的形式出現,而且在常規的工作中,為了簡單,方便,快捷,基本都是使用string類,很少有人會使用C庫中的字符串操作函數。

1.標準庫中的string類

1.string類是表示字符串的字符種類。

2.該類的接口與常規容器的接口基本相同,在添加一些專門用來操作string的常規操作。

3.string的底層是:basic_string模板類的別名。

4.不能操作多字節或者變長字符的序列。

在使用string的時候,必需包含頭文件#include以及using namespace std;

2.string類的常用接口說明

2.1 string對象的常見構造

string() 構造空的string類對象
string(const char* str) 以常量字符創為參數構造string類
string(size_t n,char ch) string對象中包含了n個字符c
string(const string& s)

拷貝構造函數

#include
#include
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;
}
 
[點擊并拖拽以移動]
?

?運行結果:

2.2 string類對象的容量操作?

size 返回字符串的有效長度
length 和size一致,推薦使用size
capacity 返回總空間大小
clear 清空有效字符,但是不對capacity有影響
reserve 為字符串預留空間
empty 判斷字符串是否為空串,返回值為bool
resize
將有效字符的個數該成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())//當sz和_capacity不相等的時候,說明reserve已經增容了。
		{
			static int n = 1;
			sz = s.capacity();
			printf("第%d次開辟空間:_capacity=%d\n", n++, sz);
		}
	}
}

運行結果:

說明在VS的環境下,reserve每次開辟的空間是前一次空間的約1.5倍。

2.2.2 clear 和 empty

?void clear ()

bool empty() const

#include
#include
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;
}

運行結果:

說明了clear只會清理有效的字符串,不會對空間的大小有影響,當clear后,empty的返回值為0,說明了此時的是s1是空字符串。

2.2.3 resize的用法

void resize(size_t n)

void resize(size_t n,char ch)

#include
#include
using namespace std;
int main()
{
	string s1("CSDN");
	s1.resize(10, 'A');//輸出的是------>CSDNAAAAAAAA
	cout <

2.3 string類對象的訪問以及遍歷操作?

operator[ ]
返回 pos 位置的字符, const string 類對象調用
begin+end
begin 獲取一個字符的迭代器 + end 獲取最后一個字符下一個位置的迭代器 (正向迭代器)
rbegin+rend
begin 獲取一個字符的迭代器 + end 獲取最后一個字符下一個位置的迭代器 (反向迭代器)
范圍for C++11支持的更簡潔的遍歷方式

const_iterator begin()const

iterator begin()

#include
#include
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++;
	}
}

運行結果:

范圍for的使用

#include
#include
using namespace std;
int main()
{
	string s1("hello CSDN");
	for (auto ch :s1)
		cout << ch<< " ";
}

本質上,范圍for調用的是迭代器。

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
#include
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;
}

運行結果:

operator +=可以尾插字符,也可以尾插字符串。實際上,operator +=尾插字符的時候,調用的是push_back,尾插字符串的時候,調用的是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
#include
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個字符
}

運行結果:

2.5 string非成員函數重載

operator+ 盡量少用,因為傳值返回,導致深拷貝效率低
operator<< 輸出運算符重載
operator>> 輸出運算符重載
getline 獲取一行字符串
relational operator 大小比較

總結

原文鏈接:https://blog.csdn.net/m0_56695823/article/details/123294701

欄目分類
最近更新