STL学习笔记--常用容器一

一、STL初识

1. STL从广义上分为: 容器(container)、算法(algorithm)、迭代器(iterator)

2. STL几乎所有的代码都采用了模板类或者模板函数

3. STL大体分为六大组件,分别是:容器、算法、迭代器、仿函数、适配器、空间配置器

  • 容器:各种数据结构,如vector、list、deque、set、map等,用来存放数据。
  • 算法:各种常用的算法,如sort、find、copy、for_each等
  • 迭代器:扮演了容器与算法之间的胶合剂。
  • 仿函数:行为类似函数,可作为算法的某种策略。
  • 适配器:一种用来修饰容器或者仿函数或迭代器接口的东西。
  • 空间配置器:负责空间的配置与管理。

4. 容器

  • STL容器就是将运用最广泛的一些数据结构实现出来
  • 常用的数据结构:数组, 链表,树, 栈, 队列, 集合, 映射表
  • 这些容器分为序列式容器关联式容器两种:
    序列式容器: 强调值的排序,序列式容器中的每个元素均有固定的位置。
    关联式容器: 二叉树结构,各元素之间没有严格的物理上的顺序关系。

5. 算法

算法分为:质变算法非质变算法
质变算法:是指运算过程中会更改区间内的元素的内容。例如拷贝,替换,删除等等。
非质变算法:是指运算过程中不会更改区间内的元素内容。例如查找、计数、遍历、寻找极值等等。

6. 迭代器

  • 每个容器都有自己专属的迭代器
  • 迭代器使用非常类似于指针,初学阶段我们可以先理解迭代器为指针
  • 种类:
种类 功能 支持运算
输入迭代器 对数据的只读访问 只读,支持++、==、!=
输出迭代器 对数据的只写访问 只写,支持++
前向迭代器 读写操作,并能向前推进迭代器 读写,支持++、==、!=
双向迭代器 读写操作,并能向前和向后操作 读写,支持++、–,
随机访问迭代器 读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器 读写,支持++、–、[n]、-n、<、<=、>、>=

二、VECTOR容器

1. vector容器基本概念

功能:

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

vector与普通数组区别:

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

动态扩展:

  • 并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间
  • vector容器的迭代器是支持随机访问的迭代器

2. vector容器存在内置数据类型

#include <iostream>
#include <vector>//vector头文件
#include <algorithm>//算法头文件

using namespace std;

void print(int val){
	cout << val << endl;
}
void test(){
	vector<int> v;//创建vector容器对象
	v.push_back(10);//向容器中存放数据
	v.push_back(20);
        
        //v.begin()返回迭代器,这个迭代器指向容器中第一个数据   
        //v.end()返回迭代器,这个迭代器指向容器元素的最后一个元素的下一个位置   
        //vector::iterator 拿到vector这种容器的迭代器类型
        
	vector<int>::iterator itBegin = v.begin();
	vector<int>::iterator itEnd = v.end();
        
        //第一种遍历方法
	while (itBegin != itEnd){
		cout << *itBegin << endl;;
		itBegin++;
	}
        cout << endl;

        //第二种
	for (vector<int>::iterator itBegin = v.begin(); itBegin !=v.end();itBegin++)   
        cout << *itBegin << endl;
        cout << endl;
        
        //第三种,使用STL提供的标准遍历算法函数,需要使用算法头文件
	for_each(v.begin(), v.end(), print);
        cout << endl;
}
int main(){
	test();

	system("pause");
	return 0;
}

运行效果:
STL学习笔记--常用容器一_第1张图片

3. vector容器存放自定义数据类型

#include <iostream>
#include <vector>

using namespace std;

class Person{
public:
	Person(string name, int age){
		this->name = name;
		this->age = age;
	}
	string name;
	int age;
};

void test(){
	vector<Person> v;

	Person p1("a", 18);
	Person p2("b", 18);
	Person p3("c", 18);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);

	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++){
                cout << (*it).name << " " << (*it).age<<endl;//第一种指代方式
		cout << it->name << " " << it->age << endl;//第二种
        }
        cout << endl;

	vector<Person *> vv;

	vv.push_back(&p1);
	vv.push_back(&p2);
	vv.push_back(&p3);

	for (vector<Person *>::iterator it = vv.begin(); it != vv.end(); it++) 
                 cout << (*it)->name << " " <<(*it)->age<<endl;//仅可用这一种指代
}z

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第2张图片

4. vector容器嵌套容器

#include <iostream>
#include <vector>

using namespace std;

void test(){
	vector<vector<int>>v;
	vector<int> v1;
	vector<int> v2;
        
	for (int i = 0; i < 4; i++){
		v1.push_back(i + 1);
		v2.push_back(i + 2);
	}
        
	v.push_back(v1);
	v.push_back(v2);
        
	for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++){
		for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
			cout << *vit << " ";
		cout << endl;
	}
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第3张图片

5. vector容器构造函数

函数原型:

  • vector v; 采用模板实现类实现,默认构造函数。

  • vector(v.begin(), v.end()); 将v[begin(), end())区间中的元素拷贝给本身。

  • vector(n, elem); 构造函数将n个elem拷贝给本身。

  • vector(const vector &vec); 拷贝构造函数。

#include <iostream>
#include <vector>

using namespace std;

void test(){
	vector<int>v1;
	for (int i = 0; i < 10; i++) v1.push_back(i);
        
	for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) cout << *it<<" ";
	cout << endl;
        
	vector<int>v2(v1);//将v1里的数赋值给v2
	for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++) cout << *it<<" ";
	cout << endl;
        
	vector<int>v3(10, 100);//向容器中存放10个100
	for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++) cout << *it << " ";
	cout << endl;
        
	vector<int>v4(v3.begin(), v3.end());将v3这段区间的数赋值给v4
	for (vector<int>::iterator it = v4.begin(); it != v4.end(); it++) cout << *it << " ";
	cout << endl;
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第4张图片

6.vector容器赋值操作

函数原型:

  • vector& operator=(const vector &vec);重载等号操作符。

  • assign(beg, end); 将[beg, end)区间中的数据拷贝赋值给本身。

  • assign(n, elem); 将n个elem拷贝赋值给本身。

#include <iostream>
#include <vector>

using namespace std;

void test(){
	vector<int>v1;
	for (int i = 0; i < 10; i++) v1.push_back(i);
	for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) cout << *it<<" ";
	cout << endl;
        
	vector<int>v2;
	v2 = v1;
	for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++) cout << *it<<" ";
	cout << endl;
        
	vector<int>v3;
	v3.assign(v1.begin(), v1.end());//vector容器自带函数
	for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++) cout << *it << " ";
	cout << endl;
        
	vector<int>v4;
	v4.assign(10, 100);
	for (vector<int>::iterator it = v4.begin(); it != v4.end(); it++) cout << *it << " ";
	cout << endl;
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第5张图片

7. vector容器容量和大小

函数原型:

  • empty(); 判断容器是否为空。

  • capacity(); 容器的容量。

  • size(); 返回容器中元素的个数。

  • resize(int num); 重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
    如果容器变短,则末尾超出容器长度的元素被删除。

  • resize(int num, elem); 重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
    如果容器变短,则末尾超出容器长度的元素被删除。

#include <iostream>
#include <vector>

using namespace std;

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

void test(){
	vector<int>v1;
	for (int i = 0; i < 10; i++) v1.push_back(i);
	print(v1);
        
	if (v1.empty()) cout << "1" << endl;//empty()是判断vector容器是否为空的函数
	else cout << "0" << endl;
        
	cout << "rongliang:" << v1.capacity() << endl;//capacity为vector容器的容量,是衡量vector容器能存放元素多少的量
	cout << "daixiao:" << v1.size() << endl;//size是大小,代指是vector容器现有元素的个数;
        
	v1.resize(15);//将vector容器的大小设定为15,多余的容量元素将设为0
	print(v1);
        
	v1.resize(16,10);//将vector容器的大小设定为16,多余的容量元素将设为10
	print(v1);
        
	v1.resize(5);//将vector容器的大小设定为5,多余的元素将被删去
	print(v1);
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第6张图片

8.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>
#include <vector>

using namespace std;

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

void test(){
	vector<int>v1;
	for (int i = 0; i < 10; i++) v1.push_back(i);
	print(v1);
        
	v1.insert(v1.begin(), 2, 100);//在vector容器的开头位置插入两个元素100
	print(v1);
        
	v1.erase(v1.begin());//删除vector容器的开头位置所对应的元素
	print(v1);
        
	v1.erase(v1.begin(),v1.end());//删除vector区间内的元素
	//v1.clear();//删除vector容器所有元素
	print(v1);
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第7张图片

9.vector容器数据存储

函数原型:

  • at(int idx); 返回索引idx所指的数据。

  • operator[]; 返回索引idx所指的数据。

  • front(); 返回容器中第一个数据元素。

  • back(); 返回容器中最后一个数据元素。

#include <iostream>
#include <vector>

using namespace std;

void test(){
	vector<int>v1;
	for (int i = 0; i < 10; i++) v1.push_back(i);
        
	for (int i = 0; i < 10; i++) cout << v1[i] << " ";//第一种类似数组的表达方式
	cout << endl;
        
	for (int i = 0; i < 10; i++) cout << v1.at(i)<< " ";//第二种
	cout << endl;
        
	cout << v1.back() << endl;//vector容器尾端对应的元素
	cout << v1.front() <<endl;//vector容器首端对应的元素
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第8张图片

10. vector容器数据交换

函数原型:

  • swap(vec); 将vec与本身的元素互换。
#include <iostream>
#include <vector>

using namespace std;

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

void test(){
	vector<int>v1;
	for (int i = 0; i < 10; i++) v1.push_back(i);
	print(v1);
        
	vector<int>v2;
	for (int i = 10; i > 0; i--) v2.push_back(i);
	print(v2);
        
	v1.swap(v2);//将v1中的元素与v2交换
	print(v1);
	print(v2);
}

void test2(){
	vector<int>v1;
	for (int i = 0; i < 100000; i++) v1.push_back(i);
	cout << "rongliang:" << v1.capacity() << endl;
        /*vector容器会为预留跟大小接近数值的容量,但当大小太大时,
        vector容器不知道需要预留多大的容量,此时为提供比大小多很多的容量*/
	cout << "daixiao:" << v1.size() << endl;
        
	v1.resize(3);//当容量太大时,这种方法只能改变vector容器的大小,但容量不会变化
	cout << "rongliang:" << v1.capacity() << endl;
	cout << "daixiao:" << v1.size() << endl;
        
	vector<int>(v1).swap(v1);/*(v1)是匿名对象,利用v1创建了一个新的对象:
        用拷贝构造函数来创建一个新的对象,新的对象没有名称 ,
        即按照v目前所用的个数来初始化匿名对象的大小。 即大小,容量分配都是3*/
	cout << "rongliang:" << v1.capacity() << endl;
	cout << "daixiao:" << v1.size() << endl;
}
int main(){
	test();
        cout<<endl;
        test2()

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第9张图片

11.vector容器预留空间

函数原型:

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

using namespace std;

void test(){
	vector<int>v1;
	int num = 0, *p = NULL;
	for (int i = 0; i < 10000; i++) {
		v1.push_back(i);
		if (p != &v1[0]){
			p = &v1[0];
			num++;
		}
	}
	cout << num << endl;
        /*当vector容器不知道要存入多少元素时,会先设定一个较小的容量,
        当容量不够时,会再开辟一个容量更大的空间,将vector里的元素存放进去,
        如此反复直至容量大于或等于元素数量*/

	v1.reserve(10000);
	num = 0;
	for (int i = 0; i < 10000; i++) {
		v1.push_back(i);
		if (p != &v1[0]){
			p = &v1[0];
			num++;
		}
	}
	cout << num << endl;

	v1.reserve(1000);
	num = 0;
	for (int i = 0; i < 10000; i++) {
		v1.push_back(i);
		if (p != &v1[0]){
			p = &v1[0];
			num++;
		}
	}
	cout << num << endl;
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第10张图片

二、STRING容器

1. string容器基本概念

本质:

  • string是C++风格的字符串,而string本质上是一个

string和char * 区别:

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

特点:

  • string 类内部封装了很多成员方法

例如:查找find,拷贝copy,删除delete 替换replace,插入insert

  • string管理char*所分配的内存,不用担心复制越界取值越界等,由类内部进行负责

2.string容器构造函数

函数原型:

  • string(); 创建一个空的字符串 例如: string str。

  • string(const char* s); 使用字符串s初始化。

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

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

#include <iostream>
#include <string>

using namespace std;

void test(){
	const char* str = "abcd";
        
	string s2(str);
	string s3(s2);
	string s4(10, 'a');
	cout << s2 << " " << s3 << " " << s4;
}

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

运行效果:

STL学习笔记--常用容器一_第11张图片

3.string容器赋值操作

函数原型:

  • 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>
#include <string>

using namespace std;

void test(){
	string s1 = "abcd";
	string s2 = s1;
        
	string s3;
	s3 = 'a';
        
	string s4;
	s4.assign("abcd");
        
	string s5;
	s5.assign(s1);
        
	string s7;
	s7.assign("abcdef", 5);
        
	string s6;
	s6.assign(10, 'a');
        
	cout << s1 << " " << s2 << " " << s3 << " " 
        << s4 << " " << s5 << " " << s6 << " " << s7;
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第12张图片

4.string容器字符串拼接

函数原型:

  • 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>
#include <string>

using namespace std;

void test(){
	string str1;
	str1 += "hh";
	cout << str1 << endl;
        
	str1 += 'a';
	cout << str1 << endl;
        
	str1 += str1;
	cout << str1 << endl;
        
	str1.append("hh");
	cout << str1 << endl;
        
	str1.append("xxxx",3);
	cout << str1 << endl;
        
	str1.append(str1);
	cout << str1 << endl;
        
	str1.append(str1, 2, 5);
	cout << str1 << endl;
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第13张图片

5.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>
#include <string>

using namespace std;

void test1(){
	string str1;
	str1 = "abcdefde";
        
	int pos = str1.find("de");
	if (pos != -1) cout << pos << endl;
	else cout << "not" << endl;
        //若寻找不到,则pos值为-1
        
	pos = str1.find("g");
	if (pos != -1) cout << pos << endl;
	else cout << "not" << endl;
        
	pos = str1.rfind("de");
	if (pos != -1) cout << pos << endl;
	else cout << "not" << endl;
        
	str1 = "abcdef";
	pos = str1.rfind("de");
	if (pos != -1) cout << pos << endl;
	else cout << "not" << endl;
}

void test2(){
	string str1 = "abcdef";
	str1.replace(1, 3, "1234");
	cout << str1 << endl;
        //无论替换元素个数多少,都会将这一区间的元素替换掉
}

int main(){
	test1();
    test2();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第14张图片

6.string容器字符串比较

函数原型:

  • int compare(const string &s) const; 与字符串s比较。

  • int compare(const char *s) const; 与字符串s比较。

#include <iostream>
#include <string>

using namespace std;

void test(){
	string str1 = "hellow";
	string str2 = "hellow";
	if (str1.compare(str2) == 0) cout << "=" << endl;
	else if (str1.compare(str2) > 0) cout << ">" << endl;
	else cout << "<" << endl;
        //若str1>str2,则返回1,反则返回-1
        
	str1 = "xellow"; 
	if (str1.compare(str2) == 0) cout << "=" << endl;
	else if (str1.compare(str2) > 0) cout << ">" << endl;
	else cout << "<" << endl;
        
	str1 = "hellowe";
	if (str1.compare(str2) == 0) cout << "=" << endl;
	else if (str1.compare(str2) > 0) cout << ">" << endl;
	else cout << "<" << endl;
        
	str1 = "zzzz";
	if (str1.compare(str2) == 0) cout << "=" << endl;
	else if (str1.compare(str2) > 0) cout << ">" << endl;
	else cout << "<" << endl;
        
	str1 = "z";
	if (str1.compare(str2) == 0) cout << "=" << endl;
	else if (str1.compare(str2) > 0) cout << ">" << endl;
	else cout << "<" << endl;
        
	str1 = "h";
	if (str1.compare(str2) == 0) cout << "=" << endl;
	else if (str1.compare(str2) > 0) cout << ">" << endl;
	else cout << "<" << endl;
        
	str1 = "i";
	if (str1.compare(str2) == 0) cout << "=" << endl;
	else if (str1.compare(str2) > 0) cout << ">" << endl;
	else cout << "<" << endl;
        /*比较原则或从首元素开始比较,但长度相同时首元素一样则继续比较后面元素
        长度不一时,首字母一样且后者也一样则长度长的大或出现元素相比大的大,否则首字母大的大,*/
}
 
int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第15张图片

7.string容器字符存取

函数原型:

  • char& operator[](int n); 通过[]方式取字符。

  • char& at(int n); 通过at方法获取字符。

#include <iostream>
#include <string>

using namespace std;

void test(){
	string str = "hello";
	cout << str << endl;
        
	for (int i = 0; i < str.size(); i++){
		cout << str[i] << " ";
	}
	cout << endl;
        
	for (int i = 0; i < str.size(); i++){
		cout << str.at(i)<< " ";
	}
	cout << endl;
        
	str[0] = 'x';
	cout << str << endl;
        
	str.at(1) = 'x';
	cout << str << endl;
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第16张图片

8.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个字符。

  • string& clear(); 删除容器中所有元素。

#include <iostream>
#include <string>

using namespace std;

void test(){
	string str = "hello";
	str.insert(1, 3, '1');
	cout << str << endl;
        
	str.erase(1, 3);
	cout << str << endl;
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第17张图片

9.string容器子串

函数原型:

  • string substr(int pos = 0, int n = npos) const; 返回由pos开始的n个字符组成的字符串.
#include <iostream>
#include <string>

using namespace std;

void test(){
	string email = "[email protected]";
	string username = email.substr(0, email.find('@'));
	cout << "username: " <<username<<endl;
        
        int pos = email.find("@"); 
        username = email.substr(0, pos); 
        cout << "username: " << username << endl;
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第18张图片

三、DEQUE容器

1.deque容器基本概念

功能:

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

deque与vector区别:

  • vector对于头部的插入删除效率低,数据量越大,效率越低
  • deque相对而言,对头部的插入删除速度回比vector快
  • vector访问元素时的速度会比deque,这和两者内部实现有关

deque内部工作原理: deque内部有个中控器,维护每段缓冲区中的内容,缓冲区中存放真实数据
中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续的内存空间

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

2.deque容器构造函数

函数原型:

  • deque deq ; 默认构造形式。

  • deque(beg, end); 构造函数将[beg, end)区间中的元素拷贝给本身。

  • deque(n, elem); 构造函数将n个elem拷贝给本身。

  • deque(const deque &deq); 拷贝构造函数。

#include <iostream>
#include <deque>

using namespace std;

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

void test(){
	deque<int>d1;
	for (int i = 0; i < 10; i++) d1.push_back(i);
	print(d1);
        
	deque<int>d2(d1);
	print(d2);
        
	deque<int>d3(d1.begin(), d1.end());
	print(d3);
        
	deque<int>d4(10,100);
	print(d4);
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第19张图片

3.string容器赋值操作

函数原型:

  • deque& operator=(const deque &deq); 重载等号操作符。

  • assign(beg, end); 将[beg, end)区间中的数据拷贝赋值给本身。

  • assign(n, elem); 将n个elem拷贝赋值给本身。

#include <iostream>
#include <deque>

using namespace std;

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

void test(){
	deque<int>d1;
	for (int i = 0; i < 10; i++) d1.push_back(i);
	print(d1);
        
	deque<int>d2;
	d2 = d1;
	print(d2);
        
	deque<int>d3;       
	d3.assign(10,100);
	print(d3); 
        
	deque<int>d4;
	d4.assign(d2.begin(),d2.end());
	print(d4);
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第20张图片

4.deque容器大小操作

函数原型:

  • deque.empty(); 判断容器是否为空。

  • deque.size(); 返回容器中元素的个数。

  • deque.resize(num); 重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
    如果容器变短,则末尾超出容器长度的元素被删除。

  • deque.resize(num, elem); 重新指定容器的长度为num,若容器变长,则以elem值填充新位置。 如果容器变短,则末尾超出容器长度的元素被删除。

#include <iostream>
#include <deque>

using namespace std;

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

void test(){
	deque<int>d1;
	for (int i = 0; i < 10; i++) d1.push_back(i);
	if (d1.empty()) cout << "kong" << endl;
	else {
		print(d1);
		cout << "daixiao:" << d1.size() << endl;
                //deque容器没有容量
	}
	d1.resize(15, 1);
	print(d1);
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第21张图片

5.deque容器插入和删除

函数原型:

两端插入操作:

  • 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>
#include <deque>

using namespace std;

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

void test(){
	deque<int>d1;
	d1.push_front(10);
	d1.push_front(20);
	d1.push_back(100);
	d1.push_back(200);
	print(d1);
        
	deque<int>::iterator it= d1.begin();
	it++;
	d1.erase(it);
	d1.insert(d1.begin(), 2, 100);
	print(d1);
        
	deque<int>d2;
	d2.push_front(1);
	d2.push_front(2);
	d2.push_back(10);
	d2.push_back(20);
	print(d2);
        
	d2.insert(d2.begin(), d1.begin(), d1.end());
	print(d2);
        
	d2.clear();
	d1.erase(d1.begin(), d1.end());
	print(d1);
	print(d2);
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第22张图片

6.deque容器数据存取

函数原型:

  • at(int idx); 返回索引idx所指的数据。

  • operator[]; 返回索引idx所指的数据。

  • front(); 返回容器中第一个数据元素。

  • back(); 返回容器中最后一个数据元素。

#include <iostream>
#include <deque>

using namespace std;

void test(){
	deque<int>d1;
		d1.push_front(10);
		d1.push_front(20);
		d1.push_back(100);
		d1.push_back(200);
                
		for (int i = 0; i < d1.size(); i++){
			cout << d1[i] << " ";
		}
		cout << endl;
                
		for (int i = 0; i < d1.size(); i++){
			cout << d1.at(i) << " ";
		}
		cout << endl;
                
		cout << "1:" << d1.front() << endl;
		cout << "2:" << d1.back() << endl;
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第23张图片

7.deque容器排序操作

函数原型:

  • sort(iterator beg, iterator end) 对beg和end区间内元素进行排序。
#include <iostream>
#include <deque>

using namespace std;

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

void test(){
	deque<int>d1;
			d1.push_front(10);
			d1.push_front(20);
			d1.push_back(100);
			d1.push_back(200);
                        print(d1);
                        
			sort(d1.begin(), d1.end());
			print(d1);
}

int main(){
	test();

	system("pause");
	return 0;
}

运行效果:

STL学习笔记--常用容器一_第24张图片

你可能感兴趣的:(c++学习笔记,c++,学习)