map/multimap容器(C++)

map/multimap容器:
map基本概念:
	map中所有元素都是pair
	pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
	所有元素都会根据元素的键值排序
本质:
	map/multimap属于关联式容器,底层结构是二叉树实现
优点:
	可以根据key快速找到value值
map和multimap的区别:
	map不容许容器中有重复key值元素
	multimap允许容器中有重复的key值元素

map构造函数和赋值:
//map中所有元素都是成对出现,插入数据的时候要使用对组
map<T1, T2> mp;					//map默认构造函数
map(const map &mp);				//拷贝构造函数
map& operator=(const map &mp);	//重载等号操作符

map的大小和交换:
size();				//返回容器中元素的数目
empty();			//判断容器是否为空
swap(mp);			//交换两个集合容器

map插入和删除:
insert(elem);		//在容器中插入元素elem
clear();			//清除所有元素
erase(pos);			//删除pos迭代器所指的元素,返回下一个元素
erase(begin,end);	//删除区间[begin, end)的所有元素,返回下一个元素的迭代器
erase(key);			//删除容器中值为key的元素

map查找和统计:
find(key);			//查找key是否存在,若存在,则返回该键的元素的迭代器;若不存在,返回set.end()
cout(key);			//统计key元素的个数

map容器排序:
map容器默认排序规则为:按照key值进行 从小到大排序
利用仿函数,改变排序规则
#include <iostream>
using namespace std;
#include<string>
#include <map>
//printMap(const map<int, int> & mp)会报错???? 
printMap(map<int, int> & mp)
{
    
    
	for (map<int, int> :: iterator it = mp.begin(); it != mp.end(); it ++){
    
    
		cout << "key = " << (*it).first << "  value = " << it -> second <<endl;
	}
	cout << endl;
}

map构造函数和赋值:

void test1()
{
    
    
	map<int, int> m;
	m.insert(pair<int, int>(5, 15));
	m.insert(pair<int, int>(3, 20));
	m.insert(pair<int, int>(4, 30));
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	printMap(m);
	
	map<int, int> m2(m);
	printMap(m2);
	map<int, int> m3;
	m3 = m2;
	printMap(m3);
}

map的大小和交换:

void test2()
{
    
    
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 40));
	m.insert(pair<int, int>(3, 10));

	if (m.empty()) {
    
    
		cout << "m为空" << endl;
	}
	else {
    
    
		cout << "m大小为 : " << m.size() << endl;
	}
	map<int, int> m2;
	if (m2.empty()) {
    
    
		cout << "m为空" << endl;
	}
	else {
    
    
		cout << "m不为空" << endl;
	}
	m2.insert(pair<int, int>(13, 14));
	printMap(m);
	printMap(m2);

	m2.swap(m);
	printMap(m);
	printMap(m2);
}

map插入和删除:

void test3()
{
    
    
	map<int, int> m;
	//插入 第一种
	m.insert(pair<int, int>(1, 10));
	//第二种
	m.insert(make_pair(2, 20));
	//第三种
	m.insert(map<int, int> ::value_type(3, 30));
	//第四种,输出时,若key不存在,会自动创造出key对应的value=0
	m[4] = 40;
	printMap(m);
	//删除
	m.erase(m.begin());
	printMap(m);
	m.erase(3);
	printMap(m);
	m.erase(m.begin(), m.end());
	printMap(m);
	m.clear();
	printMap(m);
}

map查找和统计:

void test4()
{
    
    
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(map<int, int> ::value_type(4, 10));
	m[3] = 30;

	map<int, int> ::iterator pos = m.find(3);
	if (pos != m.end()) {
    
    
		cout << "key = " << pos->first << "  value = " << (*pos).second << endl;
	}
	else {
    
    
		cout << "未找到该元素" << endl;
	}

	//map0 1,   multimap可多个
	int num = m.count(4);
	cout << num << endl;
}

map容器排序:

class myCompare
{
    
    
public:
	//注意const
	bool operator()(int v1, int v2)const {
    
    
		return v1 > v2;
	}
};
void printMap(map<int, int, myCompare>& m)
{
    
    
	for (map<int, int, myCompare> ::iterator it = m.begin(); it != m.end(); it++){
    
    
		cout << "key = " << it->first << "  value = " << (*it).second << endl;
	}
	cout << endl;
}
void test5()
{
    
    
	map<int, int, myCompare> m;
	m.insert(pair<int, int>(2, 22));
	m.insert(make_pair(4, 44));
	m.insert(map<int, int> ::value_type(1, 11));
	m[3] = 33;
	printMap(m);
}
int main()
{
    
    
	test1();
	test2();
	test3();
	test4();
	test5();
	system("pause");
	return 0;
}

*黑马程序员c++课程学习练习

猜你喜欢

转载自blog.csdn.net/Fighting_gua_biu/article/details/114049765
今日推荐