stack、queue、list、set(multiset)、map容器

1 stack容器

1.1 概念

1.2 常用接口

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

int main() {
	//1.构造
	stack<int> s;		//默认构造
	stack<int> s1(s);	//拷贝构造

	//2.元素进栈
	for (int i = 10; i < 60; i += 10) {
		s.push(i);
	}

	//3.计算栈的大小
	cout << "栈的大小为:" << s.size() << endl;
	
	//4.返回栈顶元素以及出栈
	while (!s.empty()) {
		cout << "栈顶元素为:" << s.top() << endl;
		s.pop();
	}

	return 0;
}
栈的大小为:5
栈顶元素为:50
栈顶元素为:40
栈顶元素为:30
栈顶元素为:20
栈顶元素为:10

2 queue容器

2.1 概念

2.2 常用接口

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

int main() {
	//1.构造
	queue<int> q;		//默认构造
	queue<int> q1(q);	//拷贝构造

	//2.元素从队尾入队
	for (int i = 10; i < 60; i += 10) {
		q.push(i);
	}

	//3.计算队列的大小
	cout << "队列的大小为:" << q.size() << endl;
	
	//4.返回队头、队尾元素以及出队
	cout << "队尾元素为:" << q.back() << endl;
	while (!q.empty()) {
		cout << "队头元素为:" << q.front() << endl;
		q.pop();
	}

	return 0;
}
队列的大小为:5
队尾元素为:50
队头元素为:10
队头元素为:20
队头元素为:30
队头元素为:40
队头元素为:50

3 list容器

3.1 基本概念

3.2 常用接口

3.2.1 构造函数

#include<iostream>
#include<list>
using namespace std;
void printList(const list<int>&L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
int main() {

	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	cout << "方式一" << endl;
	printList(L1);

	list<int>L2(L1.begin(), L1.end());
	cout << "方式二" << endl;
	printList(L2);
	
	list<int>L3(3, 10);
	cout << "方式三" << endl;
	printList(L3);

	list<int>L4(L3);
	cout << "方式四" << endl;
	printList(L4);

	return 0;
}
方式一
10 20 30 40
方式二
10 20 30 40
方式三
10 10 10
方式四
10 10 10

3.2.2 赋值和交换

#include<iostream>
#include<list>
using namespace std;
void printList(const list<int>&L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
int main() {

	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	list<int>L2;
	L2.assign(L1.begin(), L1.end());
	cout << "方式一" << endl;
	printList(L2);
	
	list<int>L3;
	L3.assign(3, 10);
	cout << "方式二" << endl;
	printList(L3);

	list<int>L4;
	L4 = L3;
	cout << "方式三" << endl;
	printList(L4);

	cout << "交换前" << endl;
	printList(L2);
	printList(L3);
	L2.swap(L3);
	cout << "交换后" << endl;
	printList(L2);
	printList(L3);
	return 0;
}
方式一
10 20 30 40
方式二
10 10 10
方式三
10 10 10
交换前
10 20 30 40
10 10 10
交换后
10 10 10
10 20 30 40

3.2.3 大小操作

#include<iostream>
#include<list>
using namespace std;
void printList(const list<int>&L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
int main() {

	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	if (L1.empty()) {
		cout << "容器为空" << endl;
	}
	else {
		cout << "容器中的元素个数为:" << L1.size() << endl;
	}

	L1.resize(6);
	printList(L1);

	L1.resize(4);
	printList(L1);

	cout << "------" << endl;

	L1.resize(6,3);
	printList(L1);

	L1.resize(4);
	printList(L1);
	return 0;
}
容器中的元素个数为:4
10 20 30 40 0 0
10 20 30 40
------
10 20 30 40 3 3
10 20 30 40

3.2.4 插入和删除

#include<iostream>
#include<list>
using namespace std;
void printList(const list<int>& L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
int main() {

	list<int>L1;

	//1 插入
	//10 20
	L1.push_back(10);
	L1.push_back(20);
	cout << "一:";
	printList(L1);
	//40 30 10 20
	L1.push_front(30);
	L1.push_front(40);
	cout << "二:";
	printList(L1);

	//2 删除
	//40 30 10
	L1.pop_back();
	cout << "三:";
	printList(L1);
	//30 10
	L1.pop_front();
	cout << "四:";
	printList(L1);

	//3 insert插入
	//30 20 10
	list<int>::iterator it = L1.begin();
	cout << "插入的新数据为:" << *L1.insert(++it, 20) << endl;
	cout << "五:";
	printList(L1);
	//30 1 1 20 10
	it = L1.begin();
	L1.insert(++it, 2, 1);
	cout << "六:";
	printList(L1);
	//30 9 9 1 1 20 10
	it = L1.begin();
	list<int>L2(2, 9);
	L1.insert(++it, L2.begin(), L2.end());
	cout << "七:";
	printList(L1);

	//4 erase删除
	//30 9 1 1 20 10
	it = L1.begin();
	it++;
	list<int>::iterator bt = it;
	bt++;
	cout << "删除后下一个元素为:" << *L1.erase(it, bt) << endl;
	cout << "八:";
	printList(L1);
	//9 1 1 20 10
	it = L1.begin();
	cout << "下一个元素为:" << *L1.erase(it) << endl;
	cout << "九:";
	printList(L1);

	//5 remove删除
	//9 20 10
	L1.remove(1);
	cout << "十:";
	printList(L1);

	//6 清空
	L1.clear();
	cout << L1.size() << endl;

	return 0;
}
一:10 20
二:40 30 10 20
三:40 30 10
四:30 10
插入的新数据为:20
五:30 20 10
六:30 1 1 20 10
七:30 9 9 1 1 20 10
删除后下一个元素为:9
八:30 9 1 1 20 10
下一个元素的位置为:9
九:9 1 1 20 10
十:9 20 10
0

3.2.5 数据存取

#include<iostream>
#include<list>
using namespace std;
void printList(const list<int>&L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
int main() {

	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	
	cout << L1.front() << endl;
	cout << L1.back() << endl;
	return 0;
}
10
40

3.2.6 反转和排序

#include<iostream>
#include<list>
using namespace std;
void printList(const list<int>&L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
int main() {

	list<int>L1;
	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(30);
	L1.push_back(40);
	
	L1.reverse();
	printList(L1);

	L1.sort();
	printList(L1);
	return 0;
}
40 30 10 20
10 20 30 40

3.2.7 排序案例

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

class Person {
public:
	Person(string name, int age, int height) {
		m_name = name;
		m_age = age;
		m_height = height;
	}

public:
	string m_name;
	int m_age;
	int m_height;
};

void printList(const list<Person>&L) {
	for (list<Person>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << "姓名:"<< (*it).m_name<<" " << "年龄:" << (*it).m_age << " " << "身高:" << (*it).m_height << " "<<endl;
	}
}

bool comparePerson(Person& p1, Person& p2) {
	if (p1.m_age == p2.m_age) {
		return p1.m_height > p2.m_height;
	}
	return p1.m_age < p2.m_age;
}

int main() {

	list<Person>L1;
	Person p1("刘备", 20, 168);
	Person p2("关羽", 18, 179);
	Person p3("张飞", 50, 155);
	Person p4("李白", 50, 101);

	L1.push_back(p1);
	L1.push_back(p2);
	L1.push_back(p3);
	L1.push_back(p4);

	cout << "排序前:" << endl;
	printList(L1);

	cout << "排序后:" << endl;
	L1.sort(comparePerson);
	printList(L1);
	return 0;
}
排序前:
姓名:刘备 年龄:20 身高:168
姓名:关羽 年龄:18 身高:179
姓名:张飞 年龄:50 身高:155
姓名:李白 年龄:50 身高:101
排序后:
姓名:关羽 年龄:18 身高:179
姓名:刘备 年龄:20 身高:168
姓名:张飞 年龄:50 身高:155
姓名:李白 年龄:50 身高:101

4 set(multiset)容器

4.1 基本概念

4.2 常用接口

4.2.1 构造和赋值

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

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


int main() {

	//默认构造
	set<int> s1;
	s1.insert(50);
	s1.insert(40);
	s1.insert(530);
	printSet(s1);

	//拷贝构造
	set<int>s2(s1);
	printSet(s2);

	//等号
	set<int>s3;
	s3 = s2;
	printSet(s3);

	return 0;
}
40 50 530
40 50 530
40 50 530

4.2.2 大小和交换

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

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


int main() {
	set<int> s1;
	s1.insert(50);
	s1.insert(40);
	s1.insert(530);

	if (s1.empty()) {
		cout << "容器为空" << endl;
	}
	else {
		cout << "容器中的元素个数为:" << s1.size() << endl;
	}

	set<int> s2;
	s2.insert(10);
	s2.insert(30);
	s2.insert(20);

	//交换操作
	cout << "交换前" << endl;
	printSet(s1);
	printSet(s2);
	s2.swap(s1);
	cout << "交换后" << endl;
	printSet(s1);
	printSet(s2);

	return 0;
}
容器中的元素个数为:3
交换前
40 50 530
10 20 30
交换后
10 20 30
40 50 530

4.2.3 插入和删除

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

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


int main() {
	set<int> s1;
	s1.insert(50);
	s1.insert(40);
	s1.insert(530);
	s1.insert(30);
	//30 40 50 530
	printSet(s1);

	//40 50 530
	s1.erase(s1.begin());
	printSet(s1);
	//50 530
	s1.erase(40);
	printSet(s1);
	s1.erase(s1.begin(), s1.end());//等价于s1.clear();
	if (s1.empty()) {
		cout << "容器为空" << endl;
	}

	return 0;
}
30 40 50 530
40 50 530
50 530
容器为空

4.2.4 查找和统计

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


int main() {
	set<int> s1;
	s1.insert(50);
	s1.insert(40);
	s1.insert(530);
	s1.insert(30);
	//30 40 50 530
	
	set<int>::iterator pos = s1.find(40);
	if (pos == s1.end()) {
		cout << "该元素未找到" << endl;
	}
	else {
		cout << "该元素是:"<< *pos << endl;
	}

	cout << "set中有" << s1.count(40) << "个40" << endl;

	return 0;
}
该元素是:40
set中有1个40

4.2.5 改变排序规则

#include<iostream>
#include<set>
using namespace std;
class MyCompare {
public:
	bool operator()(int v1, int v2) const{
		return v1 > v2;
	}
};

void printSet(const set<int, MyCompare>& S) {
	for (set<int, MyCompare>::const_iterator it = S.begin(); it != S.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	set<int,MyCompare> s1;
	s1.insert(50);
	s1.insert(40);
	s1.insert(530);
	s1.insert(30);
	printSet(s1);

	return 0;
}
530 50 40 30

4.2.6 自定义数据类型指定排序规则

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

class Person {
public:
	Person(string name, int age, int height) {
		m_name = name;
		m_age = age;
		m_height = height;
	}

public:
	string m_name;
	int m_age;
	int m_height;
};

class ComparePerson {
public:
	bool operator()(const Person& p1,const Person& p2) const{
		if (p1.m_age == p2.m_age) {
			return p1.m_height > p2.m_height;
		}
		
		return p1.m_age <= p2.m_age;
	}
};

void printList(const set<Person, ComparePerson>& S) {
	for (set<Person, ComparePerson>::const_iterator it = S.begin(); it != S.end(); it++) {
		cout << "姓名:" << (*it).m_name << " " << "年龄:" << (*it).m_age << " " << "身高:" << (*it).m_height << " " << endl;
	}
}

int main() {

	set<Person,ComparePerson>s1;
	Person p1("刘备", 20, 168);
	Person p2("关羽", 18, 179);
	Person p3("张飞", 50, 155);
	Person p4("李白", 50, 101);

	s1.insert(p1);
	s1.insert(p2);
	s1.insert(p3);
	s1.insert(p4);

	printList(s1);
	return 0;
}
姓名:关羽 年龄:18 身高:179
姓名:刘备 年龄:20 身高:168
姓名:张飞 年龄:50 身高:155
姓名:李白 年龄:50 身高:101

4.3 set和multiset的区别

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

void printSet(const set<int>& L) {
	for (set<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void printMultiet(const multiset<int>& L) {
	for (multiset<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

void insertJudge(pair<set<int>::iterator, bool> ret) {
	if (!ret.second) {
		cout << "插入失败" << endl;
	}
	else {
		cout << "插入成功,该元素是" << *ret.first << endl;
	}
}
int main() {
	set<int> s1;
	s1.insert(50);
	s1.insert(40);
	printSet(s1);
	
	pair<set<int>::iterator,bool> ret = s1.insert(40);
	insertJudge(ret);
	ret = s1.insert(60);
	insertJudge(ret);

	multiset<int>s2;
	s2.insert(40);
	s2.insert(40);
	printMultiet(s2);

	return 0;
}
40 50
插入失败
插入成功,该元素是60
40 40

4.3.1 pair对组(拓展)

#include<iostream>
using namespace std;

int main() {
	pair<int, string> p1(2, "小明");
	pair<int, string> p2 = make_pair(3, "小亮");
	cout << p1.first << " " << p1.second << endl;
	cout << p2.first << " " << p2.second << endl;
	return 0;
}
2 小明
3 小亮

map容器

基本概念

构造和赋值

#include<iostream>
#include<map>
using namespace std;
void printMap(const map<int,int>& m) {
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
		cout << "keyֵ=" << (*it).first << "  " << "valueֵ=" << it->second << endl;
	}
	cout << endl;
}
int main() {
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(3, 30));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(4, 40));
	printMap(m);

	map<int, int>m2(m);
	printMap(m2);

	map<int, int>m3 = m;
	printMap(m3);

}
key值=1  value值=10
key值=2  value值=20
key值=3  value值=30
key值=4  value值=40

key值=1  value值=10
key值=2  value值=20
key值=3  value值=30
key值=4  value值=40

key值=1  value值=10
key值=2  value值=20
key值=3  value值=30
key值=4  value值=40

大小和交换

#include<iostream>
#include<map>
using namespace std;
void printMap(const map<int, int>& m) {
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
		cout << "keyֵ=" << (*it).first << "  " << "valueֵ=" << it->second << endl;
	}
	cout << endl;
}
int main() {
	map<int, int>m1;
	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(3, 30));
	m1.insert(pair<int, int>(2, 20));
	m1.insert(pair<int, int>(4, 40));

	if (m1.empty()) {
		cout << "容器为空" << endl;
	}
	else {
		cout << "容器中的元素个数为:" << m1.size() << endl;
	}

	map<int, int>m2;
	m2.insert(pair<int, int>(6, 60));
	m2.insert(pair<int, int>(7, 70));
	m2.insert(pair<int, int>(5, 50));
	m2.insert(pair<int, int>(8, 80));

	//交换前
	cout << "交换前" << endl;
	printMap(m1);
	printMap(m2);
	m2.swap(m1);
	cout << "交换后" << endl;
	printMap(m1);
	printMap(m2);

}
容器中的元素个数为:4
交换前
key值=1  value值=10
key值=2  value值=20
key值=3  value值=30
key值=4  value值=40

key值=5  value值=50
key值=6  value值=60
key值=7  value值=70
key值=8  value值=80

交换后
key值=5  value值=50
key值=6  value值=60
key值=7  value值=70
key值=8  value值=80

key值=1  value值=10
key值=2  value值=20
key值=3  value值=30
key值=4  value值=40

插入和删除

#include<iostream>
#include<map>
using namespace std;
void printMap(const map<int, int>& m) {
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
		cout << "key值=" << (*it).first << "  " << "value值=" << it->second << endl;
	}
	cout << endl;
}
int main() {
	map<int, int>m;

	m.insert(pair<int, int>(1, 10));
	m.insert(make_pair(3, 20));
	m.insert(map<int, int>::value_type(2, 30));
	m[5] = 70;

	cout << "m[4]的value值为" << m[4] << endl;//[]不建议插入,但[]可以利用key访问到value
	printMap(m);

	//删除
	m.erase(m.begin());
	cout << "1: ";
	printMap(m);

	m.erase(3);//按照key删除
	cout << "2: ";
	printMap(m);

	m.erase(m.begin(), m.end()); //等价于m.clear()
	printMap(m);

}
m[4]的value值为0
key值=1  value值=10
key值=2  value值=30
key值=3  value值=20
key值=4  value值=0
key值=5  value值=70

1: key值=2  value值=30
key值=3  value值=20
key值=4  value值=0
key值=5  value值=70

2: key值=2  value值=30
key值=4  value值=0
key值=5  value值=70

查找和统计

改变排序规则

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值