【map和set】

STL map

map是STL的一种有序无重复的关联容器。关联容器与序列容器不同,他们的元素是按照关键字来保存和访问的。
map提供一对一(保存的是一种key-value的pair对象,key是关键字且唯一,value是关键字对应的值)的数据处理能力,由于这个特性,它完全有在我们处理一对一数据的时候,在编程上提供快速通道
头文件 #include <map>
名字空间 using namespace std

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

int main()
{
    map<int, string> map_test1; //构造一个空的map
	map<int, string> map_test2(map_test1.begin(), map_test1.end());
    map<int, string> map_test3(map_test1);
    map<string, string> map_test4{ { "a", "aaa" }, { "b", "aaa"},{"c","ccc" }};
	map<int, string>map_test5{ pair<int, string>(1,"a"), pair<int, string>(2,"c")};
	map<int, string>map_test6{ make_pair(1, "a"), make_pair(2, "c") };
	//插入
	//1.用数组的方式插入数据
	map_test1[1] = "obj_0";
	map_test1[2]="obj_2";
	map_test1[3] ="obj_3";
	map_test1[1] = "obj_1";
	for (map<int, string>::iterator iter = map_test1.begin(); iter!=map_test1.end(); ++iter)
		cout<< "key:" << iter->first <<",value : " << iter->second <<endl;
	cout << "------------------------------------------------------------" << endl;
	//2 insert函数插入pair对象
	map_test1.insert(pair<int, string>(5, "obj_5"));
	map_test1.insert(pair<int, string>(4, "obj_4"));
	for (map<int, string> :: iterator iter = map_test1.begin(); iter!=map_test1.end(); ++iter)
		cout << "key:"<< iter->first << ", value :"<< iter->second << endl;
	cout<<"------------------------------------------------------------"<<endl; 
	//3 inser函数插入value_ type数据
	map_test1.insert(map<int, string>:: value_type(6, "obj_6"));
	map_test1.insert(map<int, string>:: value_type(7, "obj_7")); \
	map_test1.insert(map<int, string>::value_type(1, "obj_200"));
	for (map<int, string>::iterator iter = map_test1.begin(); iter!=map_test1.end(); ++iter)
		cout << "key:" << iter->first << ",value:" << iter->second<<endl;
	cout << "------------------------------------------------------------" << endl;
	//后面两种效果上完全一样的,用insert插入数据,重复关键字不能插入。数组方式可以覆盖以前的关键字对应的值
	//大小
	cout << map_test1.size() << endl;
	//map_test1.clear();
	//遍历
	//1.前向迭代器
	//2.反向迭代器
	map<int, string>:: reverse_iterator iter;
	for (iter= map_test1.rbegin(); iter != map_test1.rend(); ++iter)
		cout << "key:" << iter->first << ",value:" << iter->second << endl;
	cout << "------------------------------------------------------------" << endl;
	//3.用[]的方式
	map_test4["abc"] = "xyz";
	cout << map_test4["abc"] << endl;
	cout << map_test4.at("abc") << endl;

	//查找并获取map中的元素
	//1.用count函数判断关键字是够出现出现是1没有是0
	cout << map_test1.count(1) << endl;
	cout << map_test1.count(0) << endl;
	//2.用find返回迭代器没找到返回end( )
	map<int, string>::iterator it = map_test1.find(0);
	if (it != map_test1.end())
		cout << it->second << endl;
	else
		cout << "do not find" << endl;

	//3.
	it = map_test1.lower_bound(3);//>=
	cout << it->second << endl;
	it = map_test1.upper_bound(3);//>
	cout << it->second <<endl;

	//删除
	//用迭代器删除
	map_test1.erase(map_test1.begin());
	//用关键字删除
	map_test1.erase(2);
	//范围删除:相当于clean
	map_test1.erase(map_test1.begin(), map_test1.end());

	getchar();
	return 0;
}


multimap 多重映射

multimap允许键的值相同,因此在插入操作的时候用到insert_equak(),除此之外,基本上与map相同

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

int main()
{
	multimap<int, string> mu_test;
	mu_test.insert({ 1,"aaa" });
	mu_test.insert({ 1,"bbb" });

	getchar();
	return 0;
}


set集合

set是一种关联容器,基本功能与数组相似,其特性如下:

set以RBTree作为底层容器
所得元素只有key没有value,value就是key
不允许出现重复值 所有元素都会自动排序
不能通过选代器改变set的值,因为set的值就是键

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

int main()
{
	set<int> set_test;
	set_test.insert(5);
	set_test.insert(1);
	set_test.insert(3);
	set_test.insert(2);
	set_test.insert(5);//键值重复不会插入
	int a[] = { 7,8,9 };
	set_test.insert(a, a + 3);
	set<int>:: iterator it;
	for (it =set_test.begin(); it != set_test.end(); ++it)
		cout<< *it <<" ";
	cout << endl;
	cout <<"----------------------------------"<< endl;
	set<int>::reverse_iterator rit;
	for (rit = set_test.rbegin(); rit != set_test.rend(); ++rit)
		cout << *rit << " ";
	cout << endl;
	cout << "----------------------------------" << endl;
	
	set_test.erase(set_test.find(1));
	set_test.erase(2);

	getchar();
	return 0;
}


multiset 多重集合

multiset相对于set来说,区别就是multiset允许键值重复,在multiset中调用RBTree的insert_equal函数,其他的基本与set相同

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自然醒欧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值