C++ Development of Generic Programming Lecture 10 [map/ multimap container]

1. Basic concepts

Introduction:

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

Nature:

  • Map/multimap is an associative container , and the underlying structure is implemented with a binary tree.

advantage:

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

The difference between map and multimap :

  • The map does not allow duplicate key value elements in the container
  • multimap allows duplicate key elements in the container

 

Two, construction and assignment

Function description:

  • Construct and assign values ​​to the map container

Function prototype:

structure:

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

Assignment:

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

Example:

#include <map>

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();

	system("pause");

	return 0;
}

Summary: All elements in the map appear in pairs, and pairs should be used when inserting data

Three, size and exchange

Function description:

  • Count the size of the map container and exchange the map container

Function prototype:

  • size(); //Returns the number of elements in the container
  • empty(); //Determine whether the container is empty
  • swap(st); //Exchange two collection containers

Example:

#include <map>

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();

	system("pause");

	return 0;
}

to sum up:

  • Statistics size — size
  • Determine whether it is empty — empty
  • Exchange container — swap

Four, insert and delete

Function description:

  • The map container inserts data and deletes data

Function prototype:

  • insert(elem); //Insert elements in the container.
  • clear(); //Clear all elements
  • erase(pos); //Delete the element pointed to by the pos iterator and return the iterator of the next element.
  • erase(beg, end); //Delete all elements in the interval [beg,end), and return the iterator of the next element.
  • erase(key); //Delete the element whose value is key in the container.

Example:

#include <map>

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; 
	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();

	system("pause");

	return 0;
}

to sum up:

  • There are many ways to insert map, just remember one
  • Insert — insert
  • Delete — erase
  • Clear — clear

Five, search and statistics

Function description:

  • Find data and statistics on the map container

Function prototype:

  • find(key); //Find whether the key exists, if it exists, return an iterator of the key's elements; if it does not exist, return set.end();
  • count(key); //Count the number of elements of the key

Example:

#include <map>

//查找和统计
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);

	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();

	system("pause");

	return 0;
}

to sum up:

  • Find — find (returns an iterator)
  • Statistics — count (for map, the result is 0 or 1)
  • The map does not allow the insertion of repeated key elements, and only takes the element whose key is x the first time.

Six, sort

learning target:

  • The default sorting rule of the map container is sorting from small to large according to the key value. Master how to change the sorting rule

Main technical points:

  • Using functors, you can change the sorting rules

Example:

#include <map>

class MyCompare {
public:
	bool operator()(int v1, int v2) {
		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();

	system("pause");

	return 0;
}

to sum up:

  • Use functors to specify the sorting rules of the map container
  • For custom data types, the map must specify the sorting rules, the same as the set container

 

Guess you like

Origin blog.csdn.net/Kukeoo/article/details/114095779