C++高级_Map/multimap容器_STL标准模板库

Map容器的简介

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

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

7

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

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

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

使用的时候需要包含头文件:#include <map>

map/multimap的默认构造函数

/*map、mutimap采用模板类实现,对象的默认构造形式*/
template <class T>

mao<T1,T2> zploo;
multimap <T1,T2> zploo;

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

map对象的拷贝构造与赋值

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

map的大小

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

map的插入与迭代器

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

//1.通过pair的方式插入对象
map<int ,string> zploo;
zploo.insert(pair<int , string>(1,"小庄"));

//2.通过pair的方式插入对象
zploo.insert(make_pair(2,"小朋"));

//3.通过value——type的方式插入对象
zploo.insert(map<int , string>::value_type(3,"小龙"));

//4.通过数组的方式插入值
zploo[4] = "呵呵";

上面代码中前三种方法,采用insert() 方法,该方法返回值为 pair<iterator , bool>

第四中方法非常直观,但是存在一个性能的问题,插入3是,现在zploo中插入主键为 3 的项,如果没找到就新增一个 键值为 3,值为初始化的对组。然后再将 值 改成“呵呵” 如果发现已经存在,则修改这个键 相应的值。

string strName = zploo[2];  // 取操作或插入操作
//只有当zploo存在2 这个键是菜正确的取操作,否则会自定插入一个实例,键为 2,值为初始化值。

map与迭代器相关函数

map.begin();    //返回容器中第一个数据的迭代器
map.end();      //返回容器中最后一个数据之后的迭代器
map.rbegin();   //返回容器中倒数第一个元素的迭代器
map.rend();     //返回容器中倒数最后一个元素的后面的迭代器

map的删除与排序

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

//排序
map.<T1, T2, less<T1> > zploo;   //该容器是按键值的升序方式排序,默认就是采用升序排序
map.<T1, T2, greater<T1> > zploo; //该容器是按键值的降序方式排序

/*
less<T1> 和 greater<T1>   可以替换成其他的函数对象 functor
可编写自定义函数对象进行自定义类型的比较,使用方法与set构造时所用的函数对象一样(不了解的可以翻翻我前面发的博客)
*/

map的查找

map.find(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练习演示代码:

#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include <map>
#include <string>
#include <functional>
#include <set>

using namespace std;


void testMap()
{
	set<int, greater<int>> s;


	map<int, string, greater<int>>  m;
	//方法一
	m.insert(pair<int, string>(1, "teacher1"));
	
	//方法二
	m.insert(make_pair(2, "teacher2"));

	//方法三
	pair<map<int, string>::iterator, bool> insertResult = m.insert(map<int, string>::value_type(3, "teacher3"));
	if (insertResult.second == true) {
		cout << "插入成功" << endl;
		cout << insertResult.first->first << " ,  " << insertResult.first->second << endl;
	}
	else {
		cout << "插入失败" << endl;
		cout << insertResult.first->first << " ,  " << insertResult.first->second << endl;
	}

	insertResult =  m.insert(map<int, string>::value_type(3, "teacher33"));
	if (insertResult.second == true) {
		cout << "插入成功" << endl;
		cout << insertResult.first->first << " ,  " << insertResult.first->second << endl;
	}
	else {
		cout << "插入失败" << endl;
		cout << insertResult.first->first << " ,  " << insertResult.first->second << endl;
	}

	//方法四
	m[4] = "teacher4";
	m[4] = "teacher44"; //会强制覆盖 key

	for (map<int, string>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << it->first << " , " << it->second << endl;
	}

	cout << " --- " << endl;
	cout << m[4] << endl;


	while (!m.empty()) {
		map<int, string>::iterator it = m.begin();
		m.erase(it);
	}
	if (m.empty() == true) {
		cout << "kong" << endl;
	}

	m.clear();
	if (m.empty() == true) {
		cout << "kong" << endl;
	}

}

void testMultiMap()
{
	multimap<int, string> mm;

	mm.insert(pair<int, string>(1, "teacher11"));
	mm.insert(make_pair(2, "teacher22"));
	mm.insert(multimap<int, string>::value_type(2, "teacher2222"));

	//mm[2] = "teaher33";
	//multimap 不支持 []方法

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

}

int main(void)
{
	//testMap();
	testMultiMap();
	
	return 0;
}

multimap练习演示代码:

#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include <map>
#include <string>

using namespace std;

/*
公司有销售部(sale)(员工2名)、技术研发部
(development)(1人)、财务部(Financial)(2人)
人员信息有:姓名(name),年龄(age),电话(tel)、工资
(salary)等组成
通过 multimap进行 信息的插入、保存、显示.
*/

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

	void display()
	{
		cout << "姓名:  " << this->name << " , age:" << this->age << ", tel : " << this->tel << " salary: " << this->salary << endl;
	}

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

int main(void)
{
	Person p1("老王", 18, "12312321321", 4000);
	Person p2("包小姐", 18, "12312321321", 200000);
	Person p3("李BOSS", 58, "1231232132312321", 100000);
	Person p4("老郭", 28, "1231232132312321", 2000001);
	Person p5("张工", 27, "12312332312321", 14000);


	multimap<string, Person> companyMap; //公司的map容器

	//插入
	companyMap.insert(pair<string, Person>("sale", p2));
	companyMap.insert(pair<string, Person>("sale", p3));
	companyMap.insert(pair<string, Person>("development", p1));
	companyMap.insert(pair<string, Person>("Financial", p4));
	companyMap.insert(pair<string, Person>("Financial", p5));

	//全部遍历
	for (multimap<string, Person>::iterator it = companyMap.begin(); it != companyMap.end(); it++)
	{
		cout << "部门:" << it->first << endl;
		it->second.display();
	}

	cout << " ---- " << endl;

	//只打印 财务部
	pair<multimap<string, Person>::iterator, multimap<string, Person>::iterator> 
	range = companyMap.equal_range("Financial");

	for (multimap<string, Person>::iterator it = range.first; it != range.second; it++)
	{
		cout << "部门:" << it->first << endl;
		it->second.display();
	}


	cout << " -----" << endl;

	//方法二
	int cnt = companyMap.count("sale");

	multimap<string, Person>::iterator it = companyMap.find("sale");
	if (it != companyMap.end()) {
		for (int i = 0; i < cnt; i++, it++) {
			cout << "部门:" << it->first << endl;
			it->second.display();
		}
	}
	
	
	return 0;
}
庄朋龙
庄朋龙

一个爱生活的技术菜鸟

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注