Map和multimap容器

1、map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对。它提供基于key的快速检索能力。

2、map中key值是唯一的。集合中的元素按一定的顺序排列。元素插入过程是按排序规则插入,所以不能指定插入位置。

3、map的具体实现采用红黑树变体的平衡二叉树的数据结构。在插入操作和删除操作上比vector快。

4、map可以直接存取key所对应的value,支持[]操作符,如map[key]=value。

5、multimap与map的区别:map支持唯一键值,每个键只能出现一次;而multimap中相同键可以出现多次。multimap不支持[]操作符。

  • #include <map> 

map的插入与迭代器

  • map.insert(...);                                            //往容器插入元素,返回pair<iterator,bool>
  • 在map中插入元素的四种方式:
    • 通过pair的方式插入对象                     map.insert(  pair<int,string>(3,"小张")  );
    • 通过make_pair的方式插入对象          map.insert(make_pair(0, “小王”));
    • 通过value_type的方式插入对象          map.insert(  map<int,string>::value_type(1,"小李")  );
    • 通过数组的方式插入值                       map[2] = “小龙"; mapStu[4] = “小陈";

前三种插入方式都采用了insert()方法,返回值为pair<iterator,bool>,第四种方法虽然非常方便简单,但存在性能问题,(先查找,如果没找到key = 2,会将只为初始化的队组插到map中,然后再去修改value值,如果存在key = 2时,直接修改对应的value)

map对象的拷贝构造与赋值

  • map(const map &mp);                           //拷贝构造函数
  • map& operator=(const map &mp);        //重载等号操作符
  • map.swap(mp);                                     //交换两个集合容器

map的大小

  • map.size();              //返回容器中元素的数目
  • map.empty();           //判断容器是否为空

map的删除

  • map.clear();                        //删除所有元素
  • map.erase(pos);                 //删除pos迭代器所指的元素,返回下一个元素的迭代器。
  • map.erase(beg,end);          //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
  • map.erase(keyElem);         //删除容器中key为keyElem的对组。

map的查找

  • map.find(key);                               //查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回map.end();
  • map.count(keyElem);                    //返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。
  • map.lower_bound(keyElem);        //返回第一个key>=keyElem元素的迭代器。
  • map.upper_bound(keyElem);       //返回第一个key>keyElem元素的迭代器。
  • map.equal_range(keyElem);        //返回容器中key与keyElem相等的上下限的两个迭代器。上限是闭区间,下限是开区间,如[beg,end)。

以上函数返回两个迭代器,而这两个迭代器被封装在pair中。

//基本用法
void Fun1()
{
	cout << "数据是顺序排列的" << endl;
	map<int, string> m1;
	m1.insert(pair<int, string>(4, "小张"));
	m1.insert(make_pair( 2,"小王"));
	m1.insert(map<int, string>::value_type(0,"小吴"));
	m1[3] ="小陈";
	m1[1] = "小冯";

	for (map<int, string>::iterator it = m1.begin(); it != m1.end(); it++)
	{
		pair<int, string> pr = *it;
		int id = pr.first;
		string name = pr.second;
		cout << "ID:" << id << " 称呼:" << name << endl;
	}
	cout << endl;

	//插入异常处理
	pair<map<int, string>::iterator, bool> pairResult = m1.insert(pair<int, string>(5, "小强"));
	if (!pairResult.second)
	{
		cout << "插入不成功" << endl;
	}
	else
	{
		cout << "插入成功" << endl;
		int firstId = pairResult.first->first;
		string firstName = pairResult.first->second;
		cout << "ID:" << firstId << " Name" << firstName << endl;
	}
	cout << endl;


	for (map<int, string>::iterator it = m1.begin(); it != m1.end(); it++)
	{
		pair<int, string> pr = *it;
		int id = pr.first;
		string name = pr.second;
		cout << "ID:" << id << " 称呼:" << name << endl;
	}
	cout << endl;


	cout << "查找" << endl;
	map<int, string>::iterator it1 = m1.find(10);
	if (it1 == m1.end())
	{
		cout << "key 10的值不存在" << endl;
	}
	else
	{
		cout << it1->first << " " << it1->second << endl;
	}
	cout << endl;


	cout << "equal_range查找" << endl;
	//m1.equal_range(3);
	//using _Pairii = pair<iterator, iterator>;
	pair<map<int, string>::iterator, map<int, string>::iterator> pairResult1 = m1.equal_range(3);
	//第一个迭代器 >= 3的 位置 
	//第一个迭代器 = 3的 位置
	if (pairResult1.first == m1.end())
	{
		cout << "第一个迭代器 >= 3的 位置 不存在" << endl;
	}
	else
	{
		cout << pairResult1.first->first << " " << pairResult1.first->second << endl;
	}

	if (pairResult1.second == m1.end())
	{
		cout << "第二个迭代器 > 3的 位置 不存在" << endl;
	}
	else
	{
		cout << pairResult1.second->first << " " << pairResult1.second->second << endl;
	}
	cout << endl;


	cout << "删除" << endl;
	cout << "删除之前的大小:" << m1.size() << endl;
	//删除
	while (!m1.empty())
	{
		map<int, string>::iterator it = m1.begin();
		cout <<"删除输出:" <<it->first << " " << it->second << endl;
		m1.erase(it);
	}
	cout << "删除之后的大小:" << m1.size() << endl;
}

Map和multimap的区别案例

/*
Multimap 案例:
1个key值可以对应多个value =>分组
公司有销售部 sale (员工2名)、技术研发部 development (1人)、财务部 Financial (2人)
人员信息有:姓名,年龄,电话、工资等组成
通过 multimap进行 信息的插入、保存、显示
分部门显示员工信息
*/

class Person
{
public:
	Person()
	{
		this->m_name = "";
		this->m_age = 0;
		this->m_tle = "";
		this->m_salary = 0;
	}
	Person(string name,int age,char *tle,int salary)
	{
		this->m_name = name;
		this->m_age = age;
		this->m_tle = tle;
		this->m_salary = salary;
	}

public:
	string	m_name;
	int		m_age;
	string	m_tle;
	double	m_salary;
};


void Fun2()
{
	Person p1, p2, p3, p4, p5;

	p1.m_name = "小张";
	p1.m_age = 22;

	p2.m_name = "小王";
	p2.m_age = 25;

	p3.m_name = "小吴";
	p3.m_age = 32;

	p4.m_name = "小尹";
	p4.m_age = 27;

	p5.m_name = "小陈";
	p5.m_age = 23;

	multimap<string, Person> m1;
	//sale部门
	m1.insert(make_pair("sale", p1));
	m1.insert(make_pair("sale", p2));
	//development 部门
	m1.insert(make_pair("development", p3));
	//Financial 部门
	m1.insert(make_pair("Financial", p4));
	m1.insert(make_pair("Financial", p5));

	for (multimap<string,Person>::iterator it = m1.begin();it != m1.end();it++)
	{
		cout << it->first << ": " << it->second.m_name << " " << it->second.m_age << endl;
	}
	cout << endl;

	//各部门人数
	int num = m1.count("sale");
	cout << "sale部门人数:" << num << endl;
	int num1 = m1.count("development");
	cout << "development部门人数:" << num1 << endl;
	int num2 = m1.count("Financial");
	cout << "Financial部门人数:" << num2 << endl;
	cout << endl;

	//仅输出一个部门的成员信息
	multimap<string,Person>::iterator it = m1.find("Financial");
	int index = 0;
	while (it != m1.end() && index <num2)
	{
		cout << it->first << ": " << it->second.m_name << " " << it->second.m_age << endl;
		it++;
		index++;
	}
	cout << endl;

	//修改成员信息
	for (multimap<string,Person>::iterator it = m1.begin();it != m1.end();it++)
	{
		if (it->second.m_age == 25)
		{
			it->second.m_name = "小七";
		}
	}

	for (multimap<string, Person>::iterator it = m1.begin(); it != m1.end(); it++)
	{
		cout << it->first << ": " << it->second.m_name << " " << it->second.m_age << endl;
	}
	cout << endl;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章