60 C++ map容器

map.h

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

void map_constrcution();
void map_insert_del();
void map_find_count();
void map_compare();

map.cpp 

#include"map_exercise.h"

// 输入
// 方法1
map<int, int> input_map(map<int, int>& obj,int num = 6)
{
	for (int i = 1; i < num; i++)
	{
		obj.insert(pair<int, int>(i, i * 10));
	}

	return obj;
}

// 方法2
map<int, int> input_map_02(map<int, int>& obj, int num = 6)
{
	for (int i = 1; i < num; i++)
	{
		obj[i] = 1 * 10;
	}
	return obj;
}

// 打印
void print_map(map<int, int>& obj)
{
	for (map<int, int>::iterator i = obj.begin(); i != obj.end(); i++)
	{
		cout << i->first << ":" << i->second << "," ;
	}
	cout << endl;
}

// 构造函数
void map_constrcution() 
{
	map<int, int> m1;
	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(2, 20));
	m1.insert(pair<int, int>(3, 30));
	print_map(m1);

	map<int, int> m2;
	input_map(m2);
	print_map(m2);

	// 拷贝函数
	map<int, int> m3(m2);
	print_map(m3);

	// 赋值
	map<int, int> m4;
	m4 = m3;
	print_map(m4);

	// 判断是否为空
	if (m4.empty()) {
		cout << "map is Null." << endl;
	}
	else
	{
		cout << "map is not null." << endl;
	}

	// 大小
	cout << m4.size() << endl;// 返回有多少对键值

	// 交换
	cout << "交换前:" << endl;
	print_map(m1);
	print_map(m4);
	// 执行交换
	m1.swap(m4);
	cout << "交换后:" << endl;
	print_map(m1);
	print_map(m4);

}


// 插入和删除
void map_insert_del()
{
	map<int, int> m;

	// 插入
	// 方法1
	m.insert(pair<int, int>(1, 10));
	// 方法2
	m.insert(make_pair(2, 20));
	// 方法3
	m.insert(map<int, int>::value_type(3, 30));
	// 方法4
	m[4] = 40;// 推荐
	m[5] = 50;// 推荐
	m[6] = 60;// 推荐
	m[7] = 70;// 推荐
	m[8] = 80;// 推荐
	m[9] = 90;// 推荐

	print_map(m);//1:10,2:20,3:30,4:40,5:50,6:60,7:70,8:80,9:90,


	// 删除
	m.erase(m.begin());// 删除首位元素
	m.erase(3);// 删除指定键
	print_map(m);// 2:20,4:40,5:50,6:60,7:70,8:80,9:90,

	map<int, int>::iterator begin = m.begin();
	for (int i = 0; i < 2; i++)
	{
		begin++;
	}
	map<int, int>::iterator end = m.begin();
	for (int i = 0; i < 5; i++)
	{
		end++;
	}
	// 删除指定区域的键值
	m.erase(begin,end);// 下标2-4区间的键值
	print_map(m);//2:20,4:40,8:80,9:90,

	//清空
	m.clear();
	print_map(m);

}

void map_find_count()
{
	map<int, int> m;
	input_map_02(m,10);
	print_map(m);// 1:10,2:10,3:10,4:10,5:10,6:10,7:10,8:10,9:10,

	map<int, int>::iterator pos = m.find(3);// 3为键值
	if (pos != m.end())
	{
		cout << "找到了元素 key = " <<pos->first<<"   value = "<<pos->second << endl;
	}
	else
	{
		cout << "没找到元素。" << endl;
	}

	// 统计
	int num = m.count(3);// 统计map中有多少个值为 3 的元素,对于set而然只有两个结果,0或1,对于multimap意义比较大
	cout<< num << endl;
}

class MyCompare
{
public:
	bool operator()(int a,int b)const
	{
		return a > b;
	}
};

void map_compare()
{
	map<int, int,MyCompare> m;
	m[1] = 10;
	m[2] = 20;
	m[3] = 30;
	m[4] = 40;
	m[5] = 50;
	m[6] = 60;
	
	for (map<int, int, MyCompare>::iterator i = m.begin(); i != m.end(); i++)
	{
		cout << i->first << ":" << i->second << ",";//6:60,5:50,4:40,3:30,2:20,1:10,
	}
	cout << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值