Skip to content

Wiki

map / multimap containers

About 6 min read

Existing English translations were reused. 1 current source blocks remain in Simplified Chinese, without triggering paid translation.

1.map/ multimap容器

1.1.Basic concepts of map

Introduction:

  • All elements in a map are pairs
  • The first element in a pair is the key (key value), which serves as an index, and the second element is the value (real value)
  • All elements are automatically sorted according to the key value of the elements

Essence:

  • map/multimap belongs toassociative containersand the underlying structure is implemented using a binary tree.

Advantages:

  • The value can be quickly found based on the key.

map and multimapdifference:

  • A map does not allow duplicate key elements in the container.
  • A multimap allows duplicate key elements in the container.

1.2.Map construction and assignment

Function description:

  • Construct and assign values to the map container

Function prototype:

Construction:

  • map<T1, T2> mp; // Default constructor of map:
  • map(const map &mp); // Copy constructor

Assignment:

  • map& operator=(const map &mp); // Overload the assignment operator

Example:

#include <iostream>
#include <map>

using namespace std;

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

void test01()
{
	map<int,int>m; //默认构造
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));
	printMap(m);

	map<int, int>m2(m); //拷贝构造
	printMap(m2);

	map<int, int>m3;
	m3 = m2; //赋值
	printMap(m3);
}

int main() {

	test01();


	return 0;
}

Operation result:

key = 1 value = 10
key = 2 value = 20
key = 3 value = 30

key = 1 value = 10
key = 2 value = 20
key = 3 value = 30

key = 1 value = 10
key = 2 value = 20
key = 3 value = 30

Summary: All elements in a map appear in pairs; when inserting data, a pair must be used.

1.3.map size and swap

Function description:

  • Count the size of a map container and swap map containers

Function prototype:

  • size(); // Return the number of elements in the container
  • empty(); // Determine whether the container is empty
  • swap(st); // Swap two set containers

Example:

#include <iostream>
#include <map>

using namespace std;

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

void test01()
{
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

	if (m.empty())
	{
		cout << "m为空" << endl;
	}
	else
	{
		cout << "m不为空" << endl;
		cout << "m的大小为: " << m.size() << endl;
	}
}

//交换
void test02()
{
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

	map<int, int>m2;
	m2.insert(pair<int, int>(4, 100));
	m2.insert(pair<int, int>(5, 200));
	m2.insert(pair<int, int>(6, 300));

	cout << "交换前" << endl;
	printMap(m);
	printMap(m2);

	cout << "交换后" << endl;
	m.swap(m2);
	printMap(m);
	printMap(m2);
}

int main() {

	test01();

	test02();


	return 0;
}

Operation result:

m不为空
m的大小为: 3
交换前
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30

key = 4 value = 100
key = 5 value = 200
key = 6 value = 300

交换后
key = 4 value = 100
key = 5 value = 200
key = 6 value = 300

key = 1 value = 10
key = 2 value = 20
key = 3 value = 30

Summary:

  • Statistics size --- size
  • Determine whether it is empty --- empty
  • Swap containers --- swap

1.4.map insertion and deletion

Function description:

  • Inserting data into and deleting data from a map container

Function prototype:

  • insert(elem); //Insert an element into the container.
  • clear(); //Clear all elements
  • erase(pos); //Delete the element pointed to by the pos iterator and return an iterator to the next element.
  • erase(beg, end); //Delete all elements in the range [beg,end) and return an iterator to the next element.
  • erase(key); //Delete an element with value key from the container.

Example:

#include <iostream>
#include <map>

using namespace std;

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

void test01()
{
	//插入
	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));
	//第四种插入方式(常用)
	m[4] = 40; 
	//第五种插入方式(常用,但需要现代C++11)
	m.insert({5, 50});

	printMap(m);

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

	m.erase(3);
	printMap(m);

	//清空
	m.erase(m.begin(),m.end());
	m.clear();
	printMap(m);
}

int main() {

	test01();


	return 0;
}

Operation result:

key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40
key = 5 value = 50

key = 2 value = 20
key = 3 value = 30
key = 4 value = 40
key = 5 value = 50

key = 2 value = 20
key = 4 value = 40
key = 5 value = 50

Summary:

  • There are many ways to insert into a map; just remember one of them.
  • insert --- insert
  • delete --- erase
  • clear --- clear

1.5.map lookup and statistics

Function description:

  • Search for data and perform statistical calculations on the map container.

Function prototype:

  • find(key); // Find whether key exists. If it exists, return an iterator to the element with that key; if it does not exist, return set.end();
  • count(key); // Count the number of elements for key

Example:

#include <iostream>
#include <map>

using namespace std;

//查找和统计
void test01()
{
	map<int, int>m; 
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

	//查找
	map<int, int>::iterator pos = m.find(3);    //将pos指向3 30这个元素

	if (pos != m.end())   //表示没找到元素
	{
		cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}

	//统计
	int num = m.count(3);
	cout << "num = " << num << endl;
}

int main() {

	test01();


	return 0;
}

Operation result:

找到了元素 key = 3 value = 30
num = 1

Summary:

  • find --- find (returns an iterator)
  • count --- count (for map, the result is 0 or 1)

1.6.Sorting a map container

Learning Objectives:

  • The default sorting rule for map containers is to sort by key value from smallest to largest. Master how to change the sorting rule.

Key technical points:

  • Using functors, the sorting rule can be changed.

Example:

#include <iostream>
#include <map>

using namespace std;

class MyCompare {
public:
	bool operator()(int v1, int v2) const{
		return v1 > v2;
	}
};

void test01() 
{
	//默认从小到大排序
	//利用仿函数实现从大到小排序
	map<int, int, MyCompare> m;

	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(4, 40));
	m.insert(make_pair(5, 50));

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

	test01();


	return 0;
}

Operation result:

key:5 value:50
key:4 value:40
key:3 value:30
key:2 value:20
key:1 value:10

Summary:

  • Use a functor to specify the sorting rule of the map container.
  • For custom data types, the map must specify a sorting rule, same as the set container.