[C + + improve programming] 3.8 STL common container: map/multimap container

Posted by Jaehoon on Sun, 27 Feb 2022 16:47:37 +0100

1. string container

Please click to jump to this chapter

2. vector container

Please click to jump to this chapter

3. deque container

Please click to jump to this chapter

4. stack container

Please click to jump to this chapter

5. queue container

Please click to jump to this chapter

6. list container

Please click to jump to this chapter

7. set/multiset container

Please click to jump to this chapter

8. map/multimap container

8.1 basic concept of map

Introduction:

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

Essence:

  • map/multimap is an associative container, and its underlying structure is implemented by binary tree

advantage:

  • You can quickly find the value value according to the key value

Difference between map and multimap:

  • Duplicate values are not allowed in the map element container
  • multimap allows duplicate key value elements in the container

8.2 map construction and assignment

Function Description:

  • Construct and assign the map container

Function prototype:

  • map(T1, T2) mp;// Default constructor
  • map(const map &mp);// copy constructor
  • map& operator=(const map& mp);// Assignment: overloading the equal sign operator
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()
{
	//Create map container
	map<int, int>m;

	//Insert (must be in group)
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(4, 40));
	m.insert(pair<int, int>(3, 30));

	printMap(m);//It will be sorted automatically according to the key value from small to large

	//copy construction 
	map<int, int>m2(m);

	//assignment
	map<int, int>m3;
	m3 = m2;
}

All elements in the map appear in pairs, and pairs should be used when inserting. Syntax of group: pair < data type 1, data type 2 > (Assignment 1, assignment 2);

8.3 map size and swap

Function Description:

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

Function prototype:

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

8.4 map insertion and deletion

Function Description:

  • Insert and delete the map container

Function prototype:

  • insert(elem);// Insert element in container
  • clear();// Clear all elements in the container
  • erase(pos);// Delete the data indicated by the POS iterator and return the iterator of the next element
  • erase(beg, end);// Delete the element of the [beg, end) interval and return the iterator of the next element
  • erase(key);// Delete the element with key in the container
//Create map container
map<int, int>m;

//Different methods of insertion (the first two are recommended)
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;

//delete
m.erase(m.begin());
m.erase(3);//Delete the value with key 3

If the size of the container is 4, but use the command cout < < m [5] < < endl; Then the compiler will automatically generate a pair group with a key of 5 and a value of 0, so it is not recommended to insert in this way, but you can use the key to access the value.

8.5 map search and statistics

Function Description:

  • Search the map container for data and statistics

Function prototype:

  • find(key);// Check whether the key exists, and if so, return the iterator of the element of the key; If it does not exist, return set end()
  • count(key);// Count the number of key elements

map does not allow to insert duplicate key s, even if the value s are different; multimap allows insertion

8.6 map container sorting

Function Description:

  • The default sorting rule of map container is to sort from small to large according to the key value, but the sorting rule can be changed

Main technical points:

  • Using the imitation function, the sorting rules can be changed
class MyCompare
{
public:
	bool operator()(int v1, int v2)const
	{
		//Descending order
		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>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << (*it).first << " value = " << it->second << endl;
	}
}

8.7 case - employee grouping

Case description:

  • Today, the company recruited 10 employees (ABCDEFGHIJ). After 10 employees enter the company, which department should they be assigned to work in
  • Employee information includes: name and salary composition; The part is divided into: planning, art and R & D
  • Randomly assign department and salary to 10 employees
  • Insert information through multimap, key (department number) and value (employee)
  • Display employee information by Department

[to be continued]

Topics: C++ Container