数据结构--013--STL容器map代码学习

直接撸代码-------------------不可以吗?

#include <string>
#include <iostream>

#if 1
#include <map>
using std::map;
using std::multimap;
using std::string;
using std::pair;
using std::cout;
using std::endl;
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{ std::make_pair(1, "a"), std::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"));
	map_test1.insert(pair<int, string>(1, "obj_100"));
	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 insert函数插入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);
	//范围删除
	//map_test1.erase(map_test1.begin(), map_test1.end());

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

#endif //map 映射

认识代码就好,干嘛要敲嘛!主人------------小妲己··································

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码字界陈冠希

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

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

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

打赏作者

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

抵扣说明:

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

余额充值