English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and examples of map

map is C ++ Part of STL (Standard Template Library). map is an associative container that stores sorted key-value pairs, where each key is unique, can be inserted or deleted, but cannot be changed. However, the value associated with the key can be changed.

For example:An employee map container where the employee ID is the key and the name is the value, can be represented as:

KeyValue
101Nikita
102Robin
103Deep
104John

Syntax

template < class Key,</                     //map::key_type
           class T,</                 //map::mapped_type
           class Compare = less,</         //map::key_compare
           class Alloc = allocator<pair>    //map::allocator_type
           > class map;

parameters

key:The data type of the key to be stored in the map.

type:The data type of the value to be stored in the map.

compare:A comparison class that accepts two bool type parameters and returns a value. This parameter is optional, and the binary predicate less <" key"> is the default value.

alloc:The type of the allocator object. This parameter is optional, and the default value is the allocator.

Create map

You can easily create a map with the following statement:

typedef pair<const Key, T> value_type;

The following statement will be used to create a key type ofKey type,andvaluetypeis of typeThe map.An important point is that the keys and corresponding values in the map are always inserted in pairs, you cannot insert only keys or only values in the map.

Instance1

#include <string.h>  
#include <iostream>  
#include <map>  
#include <utility>  
using namespace std;  
int main()
{
   mapEmployees;
   // 1) using array index notation for assignment
   Employees[101] = "Nikita";
   Employees[105] = "John";
   Employees[103] = "Dolly";
   Employees[104] = "Deep";
   Employees[102] = "Aman";
   cout << "Employees[104] = << Employees[104] << endl << endl;
   cout << "Map size: " << Employees.size() << endl;
   cout << endl << "Natural order:" << endl;
   for (map::iterator ii = Employees.begin(); ii != Employees.end(); ++ii)
   {
       cout << (*ii).first << ": " << (*ii).second << endl;
   }
   cout << endl << "Reverse order:" << endl;
   for(map::reverse_iterator ii=Employees.rbegin(); ii!=Employees.rend(); ++ii)
   {
       cout << (*ii).first << ": " << (*ii).second << endl;
   }
}

Output:

Employees[104=Deep
Map size: 5
Natural order:
101: Nikita
102: Aman
103: Dolly
104: Deep
105: John
Reverse order:
105: John
104: Deep
103: Dolly
102: Aman
101: Nikita

Member functions

The following is a list of all member functions of map:

Constructor/Destructor

FunctionDescription
constructorsConstruct map
destructorsMap destructor
operator=Copy the elements of the map to another map container.

Iterator

FunctionDescription
beginReturn an iterator pointing to the first element in the map.
cbeginReturn a const iterator pointing to the first element in the map.
endReturn an iterator pointing to the end.
cendReturn a constant iterator pointing to the end.
rbeginReturn a reverse iterator pointing to the end.
rendReturn a reverse iterator pointing to the beginning.
crbeginReturn a constant reverse iterator pointing to the end.
crendReturn a constant reverse iterator pointing to the beginning.

Capacity

FunctionDescription
emptyReturn true if the map is empty.
sizeReturn the number of elements in the map.
max_sizeReturn the maximum capacity of the map.

Element access

FunctionDescription
operator[]Retrieve the element with the given key.
atRetrieve the element with the given key.

Modifier

FunctionDescription
insertInsert elements into the map.
eraseErase elements from the map.
swapSwap the contents of the map.
clearDelete all elements of the map.
emplaceConstruct a new element and insert it into the map.
emplace_hintConstruct a new element through hint and insert it into the map.

Observer

FunctionDescription
key_compReturn a copy of the key comparison object.
value_compReturn a copy of the value comparison object.

Operation mode

FunctionDescription
findSearch for an element with the given key.
countGet the number of elements that match the given key.
lower_boundReturn the lower bound of the iterator.
upper_boundReturn an iterator to the upper bound.
equal_rangeReturn the range of elements that match the given key.

Allocator

FunctionDescription
get_allocatorReturn the allocator object used to construct the map.

Non-member Overloaded Function

FunctionDescription
operator==Check if two maps are equal.
operator!=Check if two maps are equal.
operator<Check if the first map is less than other maps.
operator<=Check if the first map is less than or equal to other maps.
operator>Check if the first map is greater than other maps.
operator>=Check if the first map is greater than other maps.
swap()Swap elements of two maps.