Map和multimap容器

1、map/multimap的简介

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

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

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

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

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

     #include <map> 

 

2、map/multimap对象的默认构造

    map/multimap采用模板类实现,对象的默认构造形式:

    map<T1,T2> mapTT;

    multimap<T1,T2>  multimapTT; 

    如:

          map<int, char> mapA;

          map<string,float> mapB;

          //其中T1,T2还可以用各种指针类型或自定义类型

3、map的插入与迭代器

    map.insert(...);    //往容器插入元素,返回pair<iterator,bool>

    在map中插入元素的三种方式:

    假设  map<int, string> mapStu;

    一、通过pair的方式插入对象

              mapStu.insert(  pair<int,string>(3,"小张")  );

    二、通过pair的方式插入对象

              mapStu.inset(make_pair(-1,“校长-1”));

    三、通过value_type的方式插入对象

             mapStu.insert(  map<int,string>::value_type(1,"小李")  );

    四、通过数组的方式插入值

             mapStu[3] = “小刘";

             mapStu[5] = “小王";

        

        前三种方法,采用的是insert()方法,该方法返回值为pair<iterator,bool>

        第四种方法非常直观,但存在一个性能的问题。插入3时,先在mapStu中查找主键为3的项,若没发现,则将一个键为3,值为初始化值的对组插入到mapStu中,然后再将值修改成“小刘”。若发现已存在3这个键,则修改这个键对应的value。

                     string strName = mapStu[2];  //取操作或插入操作

       只有当mapStu存在2这个键时才是正确的取操作,否则会自动插入一个实例,键为2,值为初始化值。

 

    map<T1,T2,less<T1> > mapA;  //该容器是按键的升序方式排列元素。未指定函数对象,默认采用less<T1>函数对象。

    map<T1,T2,greater<T1>> mapB;   //该容器是按键的降序方式排列元素。

    less<T1>与greater<T1>  可以替换成其它的函数对象functor。

    可编写自定义函数对象以进行自定义类型的比较,使用方法与set构造时所用的函数对象一样。

    map.begin();  //返回容器中第一个数据的迭代器。

    map.end();  //返回容器中最后一个数据之后的迭代器。

    map.rbegin();  //返回容器中倒数第一个元素的迭代器。

    map.rend();   //返回容器中倒数最后一个元素的后面的迭代器。

 

4、map对象的拷贝构造与赋值

    map(const map &mp);                 //拷贝构造函数

    map& operator=(const map &mp);         //重载等号操作符

    map.swap(mp);                                    //交换两个集合容器

5、map的大小

     map.size(); //返回容器中元素的数目

     map.empty();//判断容器是否为空

6、map的删除

     map.clear();                 //删除所有元素

     map.erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。

     map.erase(beg,end);     //删除区间[beg,end)的所有元素  ,返回下一个元素的迭代器。

     map.erase(keyElem);     //删除容器中key为keyElem的对组。

7、map的查找

    map.find(key);   查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回map.end();

    map.count(keyElem);   //返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。

8、示例代码


// map的基本操作

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


// map 元素的添加/遍历/删除基本操作

void test()
{
	map<int,string> map1;

	// 方法1
	map1.insert(pair<int,string>(1,"teacher01"));
	map1.insert(pair<int, string>(2, "teacher02"));

	// 方法2
	map1.insert(make_pair<int, string>(3, "teacher03"));
	map1.insert(make_pair<int, string>(4, "teacher04"));

	// 方法3
	map1.insert(map<int, string>::value_type(5, "teacher05"));
	map1.insert(map<int, string>::value_type(6, "teacher06"));

	// 方法4
	map1[7] = "teacher07";
	map1[8] = "teacher08";

	// 容器的遍历
	for (map<int,string>::iterator it=map1.begin();it!=map1.end();++it)
	{
		cout << it->first << "\t" << it->second << endl;
	}
	cout << "容器遍历结束" << endl;
	
	// 容器元素的删除
	while (!map1.empty())
	{
		map<int, string>::iterator it1 = map1.begin();
		cout << it1->first << "\t" << it1->second << endl;
		map1.erase(it1);
	}
	cout << endl;
}

// 插入的 4 种方法异同
// 前 3 种方法返回值为  pair<iterator, bool>			若 key  已存在 则报错
//	第 4 种方法											若key 已存在  则修改

void test1()
{
	map<int, string> map1;

	// typedef pair<iterator, bool> _Pairib;

	// 方法1
	pair<map<int, string>::iterator, bool>  mypair1 = map1.insert(pair<int, string>(1, "teacher01"));
	map1.insert(pair<int, string>(2, "teacher02"));

	// 方法2
	pair<map<int, string>::iterator, bool>  mypair3 = map1.insert(pair<int, string>(3, "teacher03"));
//	map1.insert(make_pair<int, string>(3, "teacher03"));
	map1.insert(make_pair<int, string>(4, "teacher05"));

	// 方法3
	pair<map<int, string>::iterator, bool>  mypair5 = map1.insert(pair<int, string>(5, "teacher05"));
	if (mypair5.second != true)
	{
		cout << "key 5 插入失败" << endl;
	}
	else
	{
		cout << mypair5.first->first << "\t" << mypair5.first->second << endl;
	}

	//	map1.insert(map<int, string>::value_type(5, "teacher55"));
	pair<map<int, string>::iterator, bool>  mypair6 = map1.insert(map<int, string>::value_type(5, "teacher55"));
	if (mypair6.second != true)
	{
		cout << "key 5 插入失败" << endl;
	}
	else
	{
		cout << mypair6.first->first << "\t" << mypair6.first->second << endl;
	}


	// 方法4
	map1[7] = "teacher07";
	map1[7] = "teacher77";

	// 容器的遍历
	for (map<int, string>::iterator it = map1.begin(); it != map1.end(); ++it)
	{
		cout << it->first << "\t" << it->second << endl;
	}
	cout << "容器遍历结束" << endl;

}

void test2()
{
	map<int, string> map1;

	// 方法1
	map1.insert(pair<int, string>(1, "teacher01"));
	map1.insert(pair<int, string>(2, "teacher02"));

	// 方法2
	map1.insert(make_pair<int, string>(3, "teacher03"));
	map1.insert(make_pair<int, string>(4, "teacher04"));

	// 方法3
	map1.insert(map<int, string>::value_type(5, "teacher05"));
	map1.insert(map<int, string>::value_type(6, "teacher06"));

	// 方法4
	map1[7] = "teacher07";
	map1[8] = "teacher08";

	// 容器的遍历
	for (map<int, string>::iterator it = map1.begin(); it != map1.end(); ++it)
	{
		cout << it->first << "\t" << it->second << endl;
	}
	cout << "容器遍历结束" << endl;

	// map 的查找
	map<int, string>::iterator it2 = map1.find(100);
	if (it2 == map1.end())
	{
		cout << "key 100 的值不存在" << endl;
	}
	else
	{
		cout << it2->first << "\t" << it2->second << endl;
	}

	// equal_range   // 异常处理
	// typedef pair<iterator, iterator> _Pairii;

	pair<map<int, string>::iterator, map<int, string>::iterator>  maypair2 = map1.equal_range(5);    // 返回两个迭代器   形成一个pair
	if (maypair2.first == map1.end())
	{
		cout << "第一个迭代器 ===》 5 的位置不存在" << endl;
	}
	else
	{
		cout << maypair2.first->first << "\t" << maypair2.first->second << endl;
	}
	if (maypair2.second == map1.end())
	{
		cout << "第二个迭代器 ===》 5 的位置不存在" << endl;
	}
	else
	{
		cout << maypair2.second->first << "\t" << maypair2.second->second << endl;
	}

}

int main(void)
{
	// test();
	// test1();
	test2();
	return 0;
}


// multimap的基本操作

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

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

class Person
{
public:
	Person()
	{
	
	}
	Person(string name,int age,string tel="",double sal=0.0)
	{
		this->name = name;
		this->age = age;
		this->tel = tel;
		this->salary =sal;
	}
	~Person()
	{
	
	}

	string	name;
	int		age;
	string  tel;
	double  salary;
};

void test()
{
	Person p1("王1",31,"18892139133",5000);
	Person p2("王2", 32,"15902356458",6000);
	Person p3("张3", 33,"13543689412",4500);
	Person p4("赵4", 34,"18756234100",8000);
	Person p5("赵5", 35,"18795345211",12000);

	multimap<string, Person>  mulmap;

	// sale 部门
	mulmap.insert(make_pair("sale",p1));
	mulmap.insert(make_pair("sale", p2));

	// development 部门
	mulmap.insert(make_pair("development",p3));

	// Financial 部门
	mulmap.insert(make_pair("Financial",p4));
	mulmap.insert(make_pair("Financial", p5));

	// 按照条件   检索数据   进行修改
	for (multimap<string,Person>::iterator it=mulmap.begin();it!=mulmap.end();++it)
	{
		if (it->second.age == 32)
		{
			it->second.name = "name32";
		}
		//cout << it->second.name << "\t" << it->second.age << "\t" << it->second.salary << "\t" << it->second.tel << endl;
	}

	// 遍历容器
	for (multimap<string, Person>::iterator it = mulmap.begin(); it != mulmap.end(); ++it)
	{
		cout << it->second.name << "\t" << it->second.age << "\t" << it->second.salary << "\t" << it->second.tel << endl;
	}
}

int main(void)
{
	test();

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值