10.map容器的使用

map容器

	map中所有元素都是pair;
	pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值);
	所有元素都会根据元素的键值自动排序;
	map/multimap属于关联式容器,底层结构式用二叉树实现的;
	map容器不允许有重复key值元素;
	multimap容器允许有重复key元素。

1.构造和赋值

/*1.插入 - insert*/
/*2.拷贝构造*/
/*3.赋值 - operator= */
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>

using namespace std;

/*1.插入 - insert*/
/*2.拷贝构造*/
/*3.赋值 - operator= */

void printMap(const map<int, int>&m)
{
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << "\t" << "value = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	/*创建map容器*/
	map<int, int>m;

	/*1.插入 - insert*/
	m.insert(pair<int, int>(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(4, 40));

	printMap(m);

	/*2.拷贝构造*/
	map<int, int>m1(m);

	printMap(m1);

	/*3.赋值 - operator= */
	map<int, int>m2;
	m2 = m1;

	printMap(m2);

	return;
}



int main()
{
	test01();


	return 0;
}
运行结果

在这里插入图片描述

2.大小和交换

/*1.判断大小 - size*/
/*2.判断是否为空 - empty*/
/*3.交换容器 - swap */
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>

using namespace std;

/*1.判断大小 - size*/
/*2.判断是否为空 - empty*/
/*3.交换容器 - swap */

void printMap(const map<int, int>& m)
{
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << "\t" << "value = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	/*创建map容器*/
	map<int, int>m;

	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));

	printMap(m);

	/*2.判断是否为空 - empty*/

	cout << m.empty() << endl;

	if (0 != m.empty())
	{
		cout << "容器为空" << endl;
		return;
	}

	/*1.判断大小 - size*/
	cout << "容器大小:" << m.size() << endl;

	/*创建map容器*/
	map<int, int>m1;

	m1.insert(make_pair(4, 100));
	m1.insert(make_pair(5, 200));
	m1.insert(make_pair(6, 300));


	cout << endl;

	cout << "--------交换前--------" << endl;
	cout << "m容器元素为:" << endl;
	printMap(m);
	cout << "m1容器元素为:" << endl;
	printMap(m1);

	/*3.交换容器 - swap */
	m.swap(m1);

	cout << "--------交换后--------" << endl;
	cout << "m容器元素为:" << endl;
	printMap(m);
	cout << "m1容器元素为:" << endl;
	printMap(m1);

	return;
}

int main()
{
	test01();

	return 0;
}
运行结果

在这里插入图片描述

4.插入和删除

/*1.插入数据 - insert*/
/*2.删除数据 - erase*/
/*3.清空数据 - clear*/
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>

using namespace std;

/*1.插入数据 - insert*/
/*2.删除数据 - erase*/
/*3.清空数据 - clear*/

void printMap(const map<int, int>& m)
{
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << "\t" << "value = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	/*创建map容器*/
	map<int, int>m;

	/*1.插入数据 - insert*/
	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(4, 40));

	printMap(m);

	/*2.删除数据 - erase*/
	m.erase(m.begin());
	printMap(m);

	m.erase(3);/*按照key删除*/
	printMap(m);

	/*3.清空数据 - clear*/
	//m.erase(m.begin(), m.end());
	m.clear();
	printMap(m);

	if (0 != m.empty())
	{
		cout << "容器为空!" << endl;
	}

	return;
}

int main()
{
	test01();

	return 0;
}
运行结果

在这里插入图片描述

5.查找和统计

/*1.查找数据 - find*/
/*2.统计个数 - count*/
工程代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>

using namespace std;

/*1.查找数据 - find*/
/*2.统计个数 - count*/

void printMap(const map<int, int>& m)
{
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << "\t" << "value = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	/*创建map容器*/
	map<int, int>m;

	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(4, 40));

	printMap(m);

	map<int, int>::iterator pos = m.find(2);

	if (m.end() == pos)
	{
		cout << "没找到" << endl;
	}
	else
	{
		cout << "key = " << pos->first << "\t" << "value = " << pos->second << endl;

		/*map不允许插入重复key元素,count统计只能是0或者1*/
		/*multimap的count统计可能大于1*/
		cout << "key的元素个数:" << m.count(3) << endl;
	}

	return;
}

int main()
{
	test01();

	return 0;
}
运行结果

在这里插入图片描述

6.排序

	使用仿函数使其降序排列
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>

using namespace std;

class MyCmp
{
public:
	bool operator()(int v1, int v2) const
	{
		return v1 > v2;
	}
};

void printMap(const map<int, int, MyCmp>& m)
{
	for (map<int, int, MyCmp>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << "\t" << "value = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	/*创建map容器--仿函数(降序)*/
	map<int, int, MyCmp>m;

	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(4, 40));

	printMap(m);

	return;
}

int main()
{
	test01();

	return 0;
}
运行结果

在这里插入图片描述

7.自定义数据类型排序

代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<map>

using namespace std;

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

	string m_name;
	int m_age;
};

class MyCmp
{
public:
	bool operator()(Person p1, Person p2) const
	{
		return p1.m_age > p2.m_age;
	}
};

void printMap(const map<Person, int, MyCmp>& m)
{
	for (map<Person, int, MyCmp>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "name = " << it->first.m_name << "\t" << "age = " << it->first.m_age << "\t" << "赏金 = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	Person p1("曹操", 28);
	Person p2("刘备", 30);
	Person p3("赵云", 35);

	/*创建map容器--仿函数(降序)*/
	map<Person, int, MyCmp>m;

	m.insert(make_pair(p1, 1000));
	m.insert(make_pair(p2, 2000));
	m.insert(make_pair(p3, 3000));

	printMap(m);

	return;
}

int main()
{
	test01();

	return 0;
}
运行结果

在这里插入图片描述

  • 19
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值