12STL-map/multimap-创新互联

重新系统学习c++语言,并将学习过程中的知识在这里抄录、总结、沉淀。同时希望对刷到的朋友有所帮助,一起加油哦!

成都创新互联是一家专注于网站设计、网站制作与策划设计,濉溪网站建设哪家好?成都创新互联做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:濉溪等地区。濉溪做网站价格咨询:18980820575

每一次学习都是为了追求智慧!

写在前面,本篇章主要介绍STL中常用容器map/multimap。

1.1 map基本概念

简介:

  • map中所有元素都为键值对,即pair;
  • pair中第一个元素为key(键),起到索引作用,第二个元素为value(值);
  • map中所有元素都会跟进元素的key进行自动排序。默认升序。

本质:

  map/multimap 都属于关联式容器,底层结构是用二叉树实现的。

优点:

  可以根据key值,快速找到value值。

map和multimap区别:

  • map不允许有重复key的元素;
  • multimap允许有重复key的元素。

1.2 map构造和赋值

函数原型:

构造:

  • mapmp;//map默认构造函数:
  • map(const map &mp);          //拷贝构造函数

赋值:

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

示例:

#include#include#includeusing namespace std;

void printMap(const map& m) {
	for (map::const_iterator it = m.begin(); it != m.end(); it++) {
		cout<< "key = "<< (*it).first
			<< " value = "<< it->second
			<< endl;
	}
	cout<< endl;
}
void test() {
	// 创建map,insert插入数据,默认按key值升序排序
	// 不运行插入重复key值
	mapm;
	m.insert(pair(1, 10));
	m.insert(pair(3, 30));
	m.insert(pair(2, 20));
	m.insert(pair(4, 40));
	m.insert(pair(4, 50));
	printMap(m);

	// 拷贝构造
	mapm2(m);
	printMap(m2);

	// 赋值
	mapm3;
	m3 = m2;
	printMap(m3);
}

int main() {
	test();

	system("pause");
	return 0;
}

1.3 map大小和交换

函数原型:

  • size();           //返回容器中元素的数目
  • empty(); //判断容器是否为空
  • swap(st);        //交换两个集合容器

示例:

#include#include#includeusing namespace std;

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

void test() {
	mapm;
	m.insert(pair(1, 10));
	m.insert(pair(3, 30));
	m.insert(pair(2, 20));
	m.insert(pair(4, 40));
	printMap(m);

	if (m.empty()) {
		cout<< " map is empty"<< endl;
	}
	else {
		cout<< "map is not empty"<< endl;
		cout<< "map size: "<< m.size()<< endl;
	}
}

// 交换
void test2() {
	mapm;
	m.insert(pair(1, 10));
	m.insert(pair(3, 30));
	m.insert(pair(2, 20));
	m.insert(pair(4, 40));
	cout<< "map size: "<< m.size()<< endl;
	printMap(m);

	mapm2;
	m2.insert(pair(4, 40));
	m2.insert(pair(5, 50));
	m2.insert(pair(6, 60));
	cout<< "map size: "<< m2.size()<< endl;
	printMap(m2);

	m.swap(m2);

	cout<< "change:"<< endl;
	cout<< "map size: "<< m.size()<< endl;
	printMap(m);
	cout<< "map size: "<< m2.size()<< endl;
	printMap(m2);
}
int main() {
	test2();

	system("pause");
	return 0;
}

1.4 map插入和删除

函数原型:

  • insert(elem);                 //在容器中插入元素。要插入对组
  • clear();                         //清除所有元素
  • erase(pos);                 //删除pos迭代器所指的元素,返回下一个元素的迭代器。
  • erase(beg, end);         //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
  • erase(key);                 //删除容器中值为key的元素。

示例:

#include#include#includeusing namespace std;

//insert(elem); //在容器中插入元素。
//clear(); //清除所有元素
//erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
//erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
//erase(key); //删除容器中值为key的元素。
void printMap(const map& m) {
	for (auto item : m) {
		cout<< " key = "<< item.first
			<< " value = "<< item.second
			<< " "<< endl;
	}
	cout<< endl;
}

void test() {
	// map容器插入数据
	mapm;
	
	//第一种
	m.insert(pair(1, 10));

	//第二种
	m.insert(make_pair(2, 20));

	//第三种
	m.insert(map::value_type(3, 30));

	//第四种
	m[4] = 40;

	// 不建议用[]来插入数据
	// [] 主要用来通过key来访问到value
	//cout<< m[4]<< endl;

	printMap(m);

	//删除
	m.erase(m.begin());
	printMap(m);

	m.erase(3);//按照key来删除
	m.erase(10);//删除没有key的元素,不会报错
	printMap(m);

	//m.erase(m.begin(), m.end());// 利用迭代器来清空
	m.clear();// 清空容器
	printMap(m);
}

int main() {
	test();

	system("pause");
	return 0;
}

1.5 map查找和统计

函数原型:

  • find(key);  查找key是否存在。若存在,返回该键的元素的迭代器;若不存在,返回m.end();
  • count(key);  统计key的元素个数。map只会有0或1,multimap有0和n。

示例:

#include#include#includeusing namespace std;

void test() {
	mapm;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(3, 40));

	map::iterator it = m.find(3);
	if (it !=m.end()) {
		cout<< " find! key = "<< (*it).first
			<< " value = "<< (*it).second
			<< endl;
	}
	else {
		cout<< "not find"<< endl;
	}

	// 统计
	// 由于map只能插入不可重复key值的元素,所以统计结果只能是 0 或 1
	// multimap 可以统计出 0 或n
	int n = m.count(3);
	cout<< "find key =3 times:"<< n<< endl;

	n = m.count(5);
	cout<< "find key =5 times:"<< n<< endl;
}

int main() {
	test();

	system("pause");
	return 0;
}

输出: 

find! key = 3 value = 30
find key =3 times:1
find key =5 times:0


1.6 map容器排序

问题:

map容器默认排序是按照key从大到小排序,那如何做到改变排序规则?

方法:

利用仿函数,改变排序规则。

示例:

#include#include#includeusing namespace std;
class MyCompare {
public:
	bool operator()(int v1,int v2)const {
		return v1 >v2;
	}
};

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

void test() {
	//map 默认排序规则 从小到大
	// 利用仿函数改变排序规则 从大到小
	mapm;
	m.insert(make_pair(3, 30));
	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(4, 40));
	printMap(m);
}

int main() {
	test();

	system("pause");
	return 0;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站名称:12STL-map/multimap-创新互联
本文地址:http://scyanting.com/article/dhjjcp.html