c++之关联容器

set与mulitiset

在这里插入图片描述

用法

https://www.cnblogs.com/Wangzx000/p/16758508.html
在这里插入图片描述

map与mulitiset

在这里插入图片描述

pair与make_pair

在这里插入图片描述
在这里插入图片描述

常用方法

在这里插入图片描述

pair与makepair实例代码

#include <iostream>
#include <map>

using namespace std;

int main(int argc, const char *argv[])
{
	pair<int, int> p(10, 20);
	cout << p.first << endl;
	cout << p.second << endl;

	pair<int, pair<int, int> > p1(1, p);
	cout << (p.first < p1.first) << endl;
	cout << (p.second < p1.second.second) << endl;
	cout << "---------" << endl;
	p = make_pair<int, int>(20, 30);
	cout << p.first << endl;
	cout << p.second << endl;
	p1 = make_pair<int, pair<int, int> >(40, p);
	cout << p1.first << endl;
	cout << p1.second.first << endl;
	cout << p1.second.second << endl;

	return 0;
}

map实例代码

#include <iostream>
#include <map>

using namespace std;

int main(int argc, const char *argv[])
{
	map<int, string> myMap;
	myMap.insert(make_pair<int, string>(1, "Hello"));
	cout << myMap[1] << endl;

	//一个key值,只会有一个value
	//如果有重复key值,那么只第一个,后面的插入全部失败
	/*
	myMap.insert(make_pair<int, string>(1, "World"));
	cout << myMap[1] << endl;
	*/

	map<int, string>::iterator it = myMap.find(1);
	cout << (*it).second << endl;

	myMap.insert(make_pair<int, string>(2, "World"));
	myMap.insert(make_pair<int, string>(3, "Linux"));
	myMap.insert(make_pair<int, string>(4, "Windows"));
	myMap.insert(make_pair<int, string>(5, "Win11"));

	for (it = myMap.begin(); it != myMap.end(); ++it) {
		cout << (*it).first << " " << (*it).second << endl;
	}

	cout << "-------" << endl;
	myMap.erase(myMap.find(2));
	myMap.erase(3);
	for (it = myMap.begin(); it != myMap.end(); ++it) {
		cout << (*it).first << " " << (*it).second << endl;
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值