C++学习笔记24-STL常用容器之string、vector、deque容器

1 string 容器

1.0 string 基本概念

本质

  • string是C++风格的字符串,其本质上是一个被封装好的类

string和char*区别

  • char* 是一个指针
  • string 是一个类,类内部封装了char*,管理这个字符串,是一个char* 型的容器。

特点
string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete,替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责。


1.1 string 构造函数

构造函数原型(4个函数重载):

  • string(); 创建一个空的字符串,例如:string str;
    string(const char* s); 使用字符串s初始化 。

  • string(const string& str); 使用一个string对象初始化另一个string对象

  • string(int n, char c); 使用n个字符c初始化

示例

#include<iostream>
using namespace std;
#include<string>

void test1_01()
{
    
    
	string s1;  //默认构造
	cout << "s1 = " << s1 << endl;

	const char* str = "hello world";
	string s2(str);     //使用C++字符串初始化
	cout << "s2 = " << s2 << endl;

	string s3(s2);     //拷贝构造
	cout << "s3 = " << s3 << endl;

	string s4(3, 'h');
	cout << "s4 = " << s4 << endl;

}
int main()
{
    
    
	test1_01();
	system("pause");
	return 0;
}

1.2 string 赋值操作

赋值的函数原型(3个运算符重载 , 4个函数重载):

string& operator=(const char* s);		//char*类型字符串赋值给当前的字符串
string& operator=(const string& s);		//把字符串s赋给当前的字符串
string& operator=(char C);				//字符赋值给当前的字符串
string& assign(const char* s);			//把字符串s赋给当前的字符串
string& assign(const char* s, int n);   //把字符串s的前n个字符赋给 当前的字符串
string& assign(const string& s);		//把字符串s赋给当前字符串
string& assign(int n, char C);			//用n个字符c赋给当前字符串

示例:

#include<iostream>
using namespace std;
#include<string>
void test2_01()
{
    
    
	string str1;
	str1 = "hello world";  //string& operator=(const char* s);
	cout << "str1 = " << str1 << endl;

	string str2;
	str2 = str1;		  //string& operator=(const string& s);
	cout << "str2 = " << str2 << endl;

	string str3;
	str3 = 'c';			 //string& operator=(char c);	
	cout << "str3 = " << str3 << endl;

	string str4;
	str4.assign("hello C++");	 //string& assign(const char* s);	
	cout << "str4 = " << str4 << endl;

	string str5;
	str5.assign("hello C++", 5);	 //string& assign(const char* s, int n);	
	cout << "str5 = " << str5 << endl;

	string str6;
	str6.assign(str5);	 //string& assign(const string& s);		
	cout << "str6 = " << str6 << endl;

	string str7;
	str7.assign(5,'c');	 //string& assign(int n, char c);		
	cout << "str7 = " << str7 << endl;
}

int main()
{
    
    
	test2_01();
	system("pause");
	return 0;
}

1.3 string 字符串拼接

拼接的函数原型(3个运算符重载 , 4个函数重载):

string& operator+=(const char* str);				//重载+=操作符
string& operator+=(const char C);					//重载+=操作符
string& operator+=(const string& str);				//重载+=操作符
string& append(const char* s);						//把字符串s连接到当前字符串结尾
string& append(const char* S,int n);			    //把字符串s的前n个字符连接到当前字符串结尾
string& append(const string& s);					//同operator+=(const string& str)
string & append(const string & s, int pos, int n);  //字符串s中从pos开始的n个字符连接到字符串结尾

示例:

#include<iostream>
using namespace std;
#include<string>

void test3_01(){
    
    
	string str1 = "我";
	str1 += "爱玩游戏";				//string& operator+=(const char* str);
	cout << "str1 = " << str1 << endl;			

	str1 += ':';						//string& operator+=(const char C);
	cout << "str1 = " << str1 << endl;

	string str2 = "LOL,DNF";
	str1 += str2;						//string& operator+=(const string & str);
	cout << "str2 = " << str2 << endl;

	string str3 = "I";
	str3.append(" love ");				//string& append(const char* s)
	cout << "str3 = " << str3 << endl;

	str3.append(" game , study ", 6);	//string& append(const char* S,int n);
	cout << "str3 = " << str3 << endl;

	string str4 = str3;
	str3.append(str2);					//string& append(const string& s);	
	cout << "str3 = " << str3 << endl;

	str4.append(str2, 4, 6);			//string& append(const string & s, int pos, int n);
	cout << "str4 = " << str4 << endl;
}

int main(){
    
    
	test3_01();
	system("pause");
	return 0;
}

1.4 string 查找和替换

函数原型:

int find(const string& str, int pos = 0) const;			//查找str第一次出现位置,从pos开始查找
int find(const char* s, int pos = 0) const;				//查找s第一次出现位置从pos开始查找
int find(const char* s, int pos, int n) const;			//从pos位置查找s的前n个字符第一-次位置
int find(const char C,int pos = 0) const;				//查找字符c第一次出现位置
int rfind(const string& str, int pos = npos) const;		//查找str最后一次位 置从pos开始查找
int rfind(const char* s,int pos = npos) const;			//查找s最后一-次出现位置,从pos开始查找
int rfind(const char* s,int pos, int n) const;			//从pos查找s的前n个字符最后一-次位置
int rfind(const char C,int pos = 0) const;				//查找字符c最后一次出现位置
string& replace(int pos, int n, const string& str);     //替换从pos开始n个字符为字符串str
string& replace(int pos, int n, const char* s);			//替换从pos开始的n个字符为字符串s

示例:

#include<iostream>
using namespace std;
#include<string>

//int find(const string& str, int pos = 0) const;			//查找str第一次出现位置,从pos开始查找
//int find(const char* s, int pos = 0) const;				//查找s第一次出现位置从pos开始查找
//int find(const char* s, int pos, int n) const;			//从pos位置查找s的前n个字符第一-次位置
//int find(const char C,int pos = 0) const;				//查找字符c第一次出现位置
//int rfind(const string& str, int pos = npos) const;		//查找str最后一次位 置从pos开始查找
//int rfind(const char* s,int pos = npos) const;			//查找s最后一-次出现位置,从pos开始查找
//int rfind(const char* s,int pos, int n) const;			//从pos查找s的前n个字符最后一-次位置
//int rfind(const char C,int pos = 0) const;				//查找字符c最后一次出现位置
//string& replace(int pos, int n, const string& str);     //替换从pos开始n个字符为字符串str
//string& replace(int pos, int n, const char* s);			//替换从pos开始的n个字符为字符串s

//查找
void test4_01() {
    
    
	string str1 = "abcdefgde";       
	int pos = str1.find("de");		//find是从左往右找
	int pos2 = str1.find("deg");
	cout << pos << endl;
	cout << pos2 << endl;

	int pos3 = str1.rfind("de");	//find是从右往左找
	int pos4 = str1.find("deg");
	cout << pos3 << endl;
	cout << pos4 << endl;
}
//替换
void test4_02() {
    
    
	string str1 = "abcdefg";
	str1.replace(1, 3, "123456");
	cout << "str1 = " << str1 << endl;
}

int main() {
    
    
	
	test4_01();
	test4_02();
	system("pause");
	return 0;
}

总结:

  • find 查找是从左往右,rfind 是从右往左。
  • 查找都是返回查找到的第一个字符的位置,找不到则返回 -1。
  • replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串(并且替换的字符串长度可以和被替换的字符个数不相同)。

1.5 string 字符串比较

比较方式
字符串比较是按字符的ASCII码进行对比

  • = 返回 0;
  • > 返回 1;
  • < 返回-1;

函数原型:

int compare(const string& s) const;  //与字符串s比 较
int compare(const char* s) const;    //与字符串s比较

示例:

#include<iostream>
using namespace std;
#include<string>

//int compare(const string& s) const;  //与字符串s比 较
//int compare(const char* s) const;    //与字符串s比较
void test5_01()
{
    
    
	string str1 = "xello";
	string str2 = "hello";

	if (str1.compare(str2) == 0){
    
    
		cout << "str1 == str2" << endl;
	}
	else if (str1.compare(str2) > 0) {
    
    
		cout << "str1 > str2" << endl;
	}
	else
	{
    
    
		cout << "str1 < str2" << endl;
	}
}

int main5() {
    
    
	test5_01();
	system("pasue");
	return 0;
}

1.6 string 字符存取

string中单个字符存取方式有两种:

char& operator[](int n);  //通过[]方式获取字符
char& at(int n);		  //通过at方式获取字符

示例:

#include<iostream>
using namespace std;
#include<string>

//char& operator[](int n);  //通过[]方式获取字符
//char& at(int n);		  //通过at方式获取字符

void test6_01()
{
    
    
	string str = "hello";
	cout << "str = " << str << endl;

	//通过[]访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
    
    
		cout << str[i] << ' ';
	}
	cout << endl;

	//通过at访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
    
    
		cout << str.at(i) << ' ';
	}
	cout << endl;

	//修改
	str[0] = 'x';
	cout << "str = " << str << endl;

	str[1] = 'z';
	cout << "str = " << str << endl;
}

int main() {
    
    
	test6_01();
	system("pause");
	return 0;
}

1.7 string 插入和删除

函数原型:

string& insert(int pos, const char* s);			//插入字符串
string& insert(int pos, const string& str);		//插入字符串
string& insert(int pos, int n, char C);			//在指定位置插入n个字符c
string& erase(int pos, int n = npos);			//删除从Pos开始的n个字符

示例:

#include<iostream>
using namespace std;
#include<string>

//string& insert(int pos, const char* s);			//插入字符串
//string& insert(int pos, const string& str);		//插入字符串
//string& insert(int pos, int n, char C);			//在指定位置插入n个字符c
//string& erase(int pos, int n = npos);			//删除从Pos开始的n个字符

void test7_01(){
    
    
	string str = "hello";
	str.insert(1, "111");			//string& insert(int pos, const string& str);
	cout << "str = " << str << endl;
	str.insert(0, 3, 'a');			//string& insert(int pos, int n, char C);
	cout << "str = " << str << endl;

	//删除
	str.erase(0, 3);				//string& erase(int pos, int n = npos);	
	str.erase(1, 3);
	cout << "str = " << str << endl;
}

int main(){
    
    
	test7_01();
	system("pause");
	return 0;
}

总结:
插入和删除都是从索引0开始计算的。

1.8 string 子串

函数原型:

string substr(int pos = 0, int n = npos) const; 	//返回由pos开始的n个字符组成的字符串

示例:

#include<iostream>
using namespace std;
#include<string>

void test8_01(){
    
    
	string str = "abcdef";
	string subStr = str.substr(1, 3);
	cout << "subStr = " << subStr << endl;

	//实用操作
	string email = "[email protected]";
	//从邮件地址中获取用户名信息
	int pos = email.find('@');
	cout << pos << endl;

	string usrName = email.substr(0, pos);
	cout << "usrName = " << usrName << endl;
}
int main() {
    
    
	test8_01();
	system("pause");
	return 0;
}

2 vector容器

2.0 vector基本概念

功能

  • vector数据结构和数组非常相似,也成为单端数组

vector与普通数组的区别

  • 不同之处在于数组是静态空间,而vector可以动态扩展

动态扩展

  • 并不是在原空间的后面续接新空间,而是找更大的内存空间,然后将原数据拷贝至新空间,释放原空间。

在这里插入图片描述

  • vector容器的迭代器是支持随机访问的迭代器。


/

2.1 vector构造函数

功能: 创建vector容器

函数原型

vector<T> v;					//采用模板实现类实现,默认构造函数
vector(v.begin(), v.end());		//将v[begin,end())区间的元素拷贝给本身
vector(n, elem);				//构造函数将n个elem拷贝给本身
vector(const vector& vec);		//拷贝构造函数

示例:

#include<iostream>
using namespace std;
#include<vector>

//vector<T> v;					//采用模板实现类实现,默认构造函数
//vector(v.begin(), v.end());		//将v[begin,end())区间的元素拷贝给本身
//vector(n, elem);				//构造函数将n个elem拷贝给本身
//vector(const vector& vec);		//拷贝构造函数
void printVector1(vector<int>& v){
    
    
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test01(){
    
    
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
    
    
		v1.push_back(i + 1);
	}
	printVector1(v1);

	//通过区间方式进行构造
	vector<int> v2(v1.begin(), v1.end());
	printVector1(v2);

	vector<int> v3(10, 100);
	printVector1(v3);

	vector<int> v4(v3);
	printVector1(v4);
}

int main() {
    
    
	test01();
	system("pause");
	return 0;
}


/

2.2 vector赋值操作

函数原型:

vector operator=(const vector& vec);	//重载等号操作符
assign(begin, end);						//将[beg,end)区间中的数据拷贝赋值给本身
assign(n, elem);						//将n个elem拷贝赋值给本身

示例:

#include<iostream>
using namespace std;
#include<vector>

//vector operator=(const vector& vec);	//重载等号操作符
//assign(begin, end);						//将[beg,end)区间中的数据拷贝赋值给本身
//assign(n, elem);						//将n个elem拷贝赋值给本身
void printVector2(vector<int>& v)
{
    
    
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test2_01(){
    
    
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
    
    
		v1.push_back(i);
	}
	printVector2(v1);

	//赋值
	vector<int> v2;
	v2 = v1;
	printVector2(v2);

	//assign
	vector<int> v3;
	v3.assign(v1.begin(), v1.end());
	printVector2(v3);

	//n个elem
	vector<int> v4;
	v4.assign(10, 100);
	printVector2(v4);
}

int main(){
    
    
	test2_01();
	system("pause");
	return 0;
}

总结:赋值使用 = 或者 assign 。

2.3 vector容器和大小

函数原型:

empty();					//判断容器是否为空
capacity();					//容器的容量
size();						//返回容器中元素的个数
resize(int num);			//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
							//如果容器变短,则末尾超出容器长度的元素被删除。
resize(int num, elem); ]	//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
							//如果容器变短,则末尾超出容器长度的元素被删除

案例:

#include<iostream>
using namespace std;
#include<vector>

//empty();					//判断容器是否为空
//capacity();				//容器的容量
//size();					//返回容器中元素的个数
//resize(int num);			//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
//							//如果容器变短,则末尾超出容器长度的元素被删除。
//resize(int num, elem); ]	//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
//							//如果容器变短,则末尾超出容器长度的元素被删除

void printVector3(vector<int> v) {
    
    
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test3_01(){
    
    
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
    
    
		v1.push_back(i);
	}
	printVector3(v1);

	if (v1.empty()) {
    
    	//为真
		cout << "v1为空" << endl;
	}  
	else {
    
    
		cout << "v1不为空" << endl;
		cout << "v1容量为: " << v1.capacity() << endl;
		cout << "v1大小为: " << v1.size() << endl;
	}

	//重新指定大小
	v1.resize(15);
	printVector3(v1);
	cout << "reize为15后v1的容量为: " << v1.capacity() << endl;

	v1.resize(15, 100);
	printVector3(v1);
	
	//如果大小变小,会删除
	v1.resize(5);
	printVector3(v1);
	cout << "reize为5后v1的容量为: " << v1.capacity() << endl;

}

int main() {
    
    
	test3_01();
	system("pause");
	return 0;
}

2.4 vector插入和删除

函数原型:

push_back(ele);										//尾部插入元素ele
pop_back();											//删除最后一个元素
insert(const_iterator pos, ele);					//迭代器指向位置pos插入元素ele
insert(const_iterator pos, int count, ele);			//迭代器指向位置pos插入count个元素ele
erase(const_ iterator pos);							//删除迭代器指向的元素
erase(const_ iterator start, const_ iterator end);	//删除迭代器从start到end之间的元素
clear();											//删除容器中所有元素

示例:

#include<iostream>
using namespace std;
#include<vector>

//push_back(ele);										//尾部插入元素ele
//pop_back();											//删除最后一个元素
//insert(const_iterator pos, ele);						//迭代器指向位置pos插入元素ele
//insert(const_iterator pos, int count, ele);			//迭代器指向位置pos插入count个元素ele
//erase(const_ iterator pos);							//删除迭代器指向的元素
//erase(const_ iterator start, const_ iterator end);	//删除迭代器从start到end之间的元素
//clear();												//删除容器中所有元素

void printVector4(vector<int> v) {
    
    
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test4_01() {
    
    
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
    
    
		v1.push_back(i * 10);
	}
	printVector4(v1);

	//删除尾部元素
	v1.pop_back();
	printVector4(v1);

	//插入元素,参数是迭代器
	v1.insert(v1.begin() + 2, 100);		//在第三个位置插入元素
	printVector4(v1);

	v1.insert(v1.begin(), 2, 1000);		//在首位置插入依次2个1000
	printVector4(v1);

	//删除,参数也是迭代器
	v1.erase(v1.begin());
	printVector4(v1);

	v1.erase(v1.begin(), v1.end() - 5);   //删除指定区间的元素,留下后5个元素
	printVector4(v1);

	//清空容器
	v1.clear();
	printVector4(v1);
}

int main() {
    
    
	test4_01();
	system("pause");
	return 0;
}


2.5 vector数据存取

函数原型:

at(int idx);	//返回索引idx所指的数据
operator[];		//返回索引idx所指的数据
front();		//返回容器中第一个数据元素
back();			//返回容器中最后一个数据元素

示例:

#include<iostream>
using namespace std;
#include<vector>

//at(int idx);	//返回索引idx所指的数据
//operator[];		//返回索引idx所指的数据
//front();		//返回容器中第一个数据元素
//back();			//返回容器中最后一个数据元素

void test5_01() {
    
    
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
    
    
		v1.push_back(i);
	}

	//利用 []
	for (int i = 0; i < v1.size(); i++) {
    
    
		cout << v1[i] << " ";
	}
	cout << endl;

	//利用 at()
	for (int i = 0; i < v1.size(); i++) {
    
    
		cout << v1.at(i) << " ";
	}
	cout << endl;

	//返回第一个元素
	cout << v1.front() << endl;

	//返回最后一个元素
	cout << v1.back() << endl;

}

int main() {
    
    
	test5_01();
	system("pause");
	return 0;
}

2.6 vector互换容器、巧用swap可以收缩内存空间

vector支持直接实现两个容器内元素进行互换。

函数原型:

swap(vec); //将vec与本身的元素互换

并且通过swap()函数可以收缩vector对象的内存空间。

示例:

#include<iostream>
using namespace std;
#include<vector>

//swap(vec); //将vec与本身的元素互换
void printVector6(vector<int>& v) {
    
    
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test6_01() {
    
    
	vector<int> v1,v2;
	for (int i = 0; i < 10; i++) {
    
    
		v1.push_back(i);
		v2.push_back(9-i);
	}
	cout << "交换前" << endl;
	printVector6(v1);
	printVector6(v2);
	
	//v1.swap(v2);
	swap(v1, v2);  //两种都可以
	cout << "交换后" << endl;
	printVector6(v1);
	printVector6(v2);

}
//巧用swap可以收缩内存空间
void test6_02() {
    
    
	vector<int> v;
	for (int i = 0; i < 10000; i++) {
    
    
		v.push_back(i);
	}
	cout << "v的容量为: " << v.capacity() << endl;
	cout << "v的大小为: " << v.size() << endl;

	v.resize(3);
	cout << "v的容量为: " << v.capacity() << endl;  //容量不会收缩
	cout << "v的大小为: " << v.size() << endl;

	//用swap
	//使用匿名对象方法vector<int>(v),这是有参构造将v作为参数传入,因为只穿入元素,所以生成的对象容量和大小都是v的元素个数
	//经过交换,使得v的容量被收缩
	vector<int>(v).swap(v);
	cout << "v的容量为: " << v.capacity() << endl;
	cout << "v的大小为: " << v.size() << endl;
	//同理也可以用此方法指定v的容量和大小
	vector<int>(10).swap(v);
	cout << "v的容量为: " << v.capacity() << endl;
	cout << "v的大小为: " << v.size() << endl;
}

int main() {
    
    
	test6_01();
	test6_02();
	system("pause");
	return 0;
}

2.7 预留空间

vector中预留空间的目的就是为了较少动态扩展容量时的扩展次数。

函数原型:

reserve(int len); //容器预留len个元素长度,预留位置不会初始化,元素不可访问

示例:

#include<iostream>
using namespace std;
#include<vector>
//reserve(int len); //容器预留len个元素长度,预留位置不会初始化,元素不可访问

void test7_01() {
    
    
	vector<int> v;
	int num=0; //统计开辟次数
	int* p = NULL;
	for (int i = 0; i < 100000; i++) {
    
    
		v.push_back(i);
		if (p != &v[0]) {
    
       //每次开辟内存都会改变地址
			p = &v[0];
			num++;
		}
	}
	cout << "开辟了: " << num << "次" << endl;

}

void test7_02() {
    
    
	vector<int> v;
	v.reserve(100000);
	int num = 0; //统计开辟次数
	int* p = NULL;
	for (int i = 0; i < 100000; i++) {
    
    
		v.push_back(i);
		if (p != &v[0]) {
    
       //每次开辟内存都会改变地址
			p = &v[0];
			num++;
		}
	}
	cout << "开辟了: " << num << "次" << endl;
}
int main() {
    
    
	test7_01();
	test7_02();
	system("pause");
	return 0;
}

3 deque容器

2.0 deque基本概念

功能

  • 双端数组,可以对头端进行插入删除操作

deque与vector区别

  • vector对于头部的插入删除效率低,数据量越大,效率越低。deque相对而言,对头部的插入删除速度回比vector快。
  • vector访问元素时的速度会比deque快(需要使用地址寻找),这和两者内部实现有关。
    在这里插入图片描述

deque内部工作原理:
deque内部有个中控器,维护每段缓冲区中的内容,缓冲区中存放真实数据中。
控器维护的是每个缓冲区的地址,使得使用deque时像—片连续的内存空间。
在这里插入图片描述

  • deque容器的迭代器也是支持随机访问的。

3.1 deque构造函数

函数原型:

deque<T> deqT;			//默认构造形式
deque(beg, end);			//构造函数将[beg, end)区间中的元素拷贝给本身。
deque(n, elem);			//构造函数将n个elem拷贝给本身。
deque(const deque& deq);	//拷贝构造函数

示例:

#include<iostream>
#include<deque>
using namespace std;

void printDeque(deque<int>& d) {
    
    
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
    
    
		//*it = 100;  #使用const_iterator时不能改变它的值
		cout << *it << " ";
	}
	cout << endl;
}

void test1_01() {
    
    
	deque<int> d1;
	for (int i = 0; i < 10; i++) {
    
    
		d1.push_back(i);
	}
	printDeque(d1);

	deque<int> d2(d1.begin(),d1.end());
	printDeque(d2);

	deque<int> d3(10, 100);
	printDeque(d3);

	deque<int> d4(d3);
	printDeque(d4);


}

int main() {
    
    

	test1_01();
	system("pause");
	return 0;
}

3.2 赋值操作

函数原型(和vector一样):

deque& operator=(const deque& deq);
assign(beg, end);
assign(n, elem);  //将n个elem赋值给deque

示例:

#include<iostream>
#include<deque>
using namespace std;

void printDeque3(const deque<int>&d) {
    
    
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
    
    
		cout << *it << " ";
	}
	cout << endl;
}
void test3_01() 
{
    
    
	deque<int> d1;
	for (int i = 0; i < 10; i++) {
    
    
		d1.push_back(i);
	}
	printDeque3(d1);
	
	deque<int>d2;
	d2 = d1;
	printDeque3(d2);
	
	deque<int>d3;
	d3.assign(d1.begin(), d1.end());
	printDeque3(d3);

	deque<int>d4;
	d4.assign(10, 100);
	printDeque3(d4);
}

int main()
{
    
    
	test3_01();

	system("pause");
	return 0;
}

3.3 大小操作

函数原型:

deque.empty();				判断容器是否为空
deque.size();				返回容器中元素的个数
deque.resize(num);			重新指定容器的长度为num, 若容器变长,则以默认值0填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
deque.resize(num,elem);	重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。

示例:

#include<iostream>
#include<deque>
using namespace std;
 
void printDeque3(const deque<int>& d)
{
    
    
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
    
    
		cout << *it << " ";
	}
	cout << endl;
}
void test3_01() 
{
    
    
	deque<int> d1;
	cout << d1.empty() << endl;
	for (int i = 0; i < 10; i++)
	{
    
    
		d1.push_back(i);
	}
	cout << d1.empty() << endl;
	printDeque3(d1);
	cout << "d1的大小:" << d1.size() << endl;
	//deque没有容量大小,可以一直插入数据

	d1.resize(20);
	printDeque3(d1);
	d1.resize(25, 1);
	printDeque3(d1);
	d1.resize(5);
	printDeque3(d1);
}

int main()
{
    
    
	test3_01();
	system("pause");
	return 0;
}

3.4 插入和删除

函数原型:

两端插入操作:

push_back(elem) ;		在容器尾部添加一个数据
push_front(elem );		在容器头部插入一个数据
pop_back();				删除容器最后一个数据
pop_front( ) ;			删除容器第一个数据

指定位置操作:

insert(pos,elem); 		在pos位置插入一个elem元素的拷贝,返回新数据的位置。
insert(pos,n,elem);		在pos位置插入n个elem数据,无返回值。
insert(pos,beg,end);	在pos位置插入[beg,end)区间的数据,无返回值。
clear();				清空容器的所有数据
erase(beg,end);			删除[beg,end)区间的数据,返回下一个数据的位置。
erase(pos) ;			删除pos位置的数据,返回下一个数据的位置。

示例:

#include<iostream>
using namespace std;
#include<deque>

void printDeque4(const deque<int>& d)
{
    
    
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test4_01()
{
    
    
	deque<int> d1;
	//尾插
	d1.push_back(10);
	d1.push_back(20);

	//头插
	d1.push_front(100);
	d1.push_front(200);
	printDeque4(d1);

	//尾删
	d1.pop_back();
	printDeque4(d1);
	//头删
	d1.pop_front();
	cout << "d1" << endl;
	printDeque4(d1);
	
	deque<int> d2;
	d2.push_back(10);
	d2.push_back(20);
	d2.push_front(100);
	d2.push_front(200);
	cout << "d2" << endl;
	printDeque4(d2);
	d2.insert(d2.begin(), 1000);
	printDeque4(d2);
	d2.insert(d2.begin(), 5,10000);
	printDeque4(d2);

	//按照区间插入
	deque<int> d3;
	d3.push_back(1);
	d3.push_back(2);
	d3.push_back(3);
	cout << "d3" << endl;
	printDeque4(d3);
	d3.insert(d3.begin(), d1.begin(), d1.end()); //(pos,[begin,end))
	printDeque4(d3);

	//删除
	deque<int>::iterator it = d3.begin();
	it++;
	d3.erase(it);
	printDeque4(d3);
	//按区间方式删除
	d3.erase(d3.begin(), d3.end());  //和d3.clear()效果一样
	printDeque4(d3);

}

int main()
{
    
    
	test4_01();
	system("pause");
	return 0;
}

3.5 数据存取

函数原型:

at(int idx);		//返回索引idx所指的数据
operator[];			//返回索引idx所指的数据
front(); 			//饭回容器中第一个数据元素
back(); 			//返回容器中最后一个数据元素

示例:

#include<iostream>
#include<deque>
using namespace std;

void test5_01()
{
    
    
	deque<int> d;
	d.push_back(1);
	d.push_back(2);
	d.push_back(3);
	d.push_front(100);
	d.push_front(200);
	d.push_front(300);
	for (int i = 0; i < d.size(); i++)
	{
    
    
		cout << d[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < d.size(); i++)
	{
    
    
		cout << d.at((d.size()-1) - i) << " ";
	}
	cout << endl;

	cout << "第一个元素为:" << d.front() << endl;
	cout << "最后的元素为:" << d.back() << endl;
}

int main()
{
    
    
	test5_01();
	system("pause");
	return 0;
}

3.6 deque排序

函数原型:

#include<algorithm>
sort(iterator beg, iterator end) ll对beg和end区间内元素进行排序

对于支持随机访问迭代器的容器都可以使用sort。
随机访问迭代器的意思是可以在该迭代器指向的位置基础上向前或者向后移动n的位置,还能获取到容器的数据。
在判断的时候我们可以通过判断该容器的迭代器是否支持”+n“的操作来判断容器的迭代器是否是随机访问迭代器。vector,deque,string的迭代器支持随机访问,list的迭代器不支持

示例:

#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;

void printDeque6(deque<int>& d)
{
    
    
	for (deque<int>::iterator it = d.begin(); it != d.end(); it++)
	{
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test6_01()
{
    
    
	deque<int> d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(3);
	d.push_front(200);
	d.push_front(500);
	d.push_front(300);

	printDeque6(d);
	//默认升序
	//对于支持随机访问迭代器的容器都可以使用sort.
	//deque,vector,
	sort(d.begin(), d.end());
	printDeque6(d);
}

int main()
{
    
    
	test6_01();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_49030008/article/details/123672670
今日推荐