C++ map function

 

C++ map function

Maps are part of the C++ STL (Standard Template Library). Maps are the associative containers that store sorted key-value pair, in which each key is unique and it can be inserted or deleted but cannot be altered. Values associated with keys can be changed.

For example: A map of Employees where employee ID is the key and name is the value can be represented as:

KeysValues
101Nikita
102Robin
103Deep
104John

Syntax

  1. template < class Key,                                             // map::key_type  
  2.            class T,                                                     // map::mapped_type  
  3.            class Compare = less<Key>,                        // map::key_compare  
  4.            class Alloc = allocator<pair<const Key,T> >    // map::allocator_type  
  5.            > class map;  

Parameter

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

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

compare: A comparison class that takes two arguments of the same type bool and returns a value. This argument is optional and the binary predicate less<"key"> is the default value.

alloc: Type of the allocator object. This argument is optional and the default value is allocator .

Creating a map

Maps can easily be created using the following statement:

  1. typedef pair<const Key, T> value_type;  

The above form will use to create a map with key of type Key type and value of type value type. One important thing is that key of a map and corresponding values are always inserted as a pair, you cannot insert only key or just a value in a map.

Example 1

  1. #include <string.h>  
  2. #include <iostream>  
  3. #include <map>  
  4. #include <utility>  
  5. using namespace std;  
  6. int main()  
  7. {  
  8.    map<int, string> Employees;  
  9.    // 1) Assignment using array index notation  
  10.    Employees[101] = "Nikita";  
  11.    Employees[105] = "John";  
  12.    Employees[103] = "Dolly";  
  13.    Employees[104] = "Deep";  
  14.    Employees[102] = "Aman";  
  15.    cout << "Employees[104]=" << Employees[104] << endl << endl;  
  16.    cout << "Map size: " << Employees.size() << endl;  
  17.    cout << endl << "Natural Order:" << endl;  
  18.    for( map<int,string>::iterator ii=Employees.begin(); ii!=Employees.end(); ++ii)  
  19.    {  
  20.        cout << (*ii).first << ": " << (*ii).second << endl;  
  21.    }  
  22.    cout << endl << "Reverse Order:" << endl;  
  23.    for( map<int,string>::reverse_iterator ii=Employees.rbegin(); ii!=Employees.rend(); ++ii)  
  24.    {  
  25.        cout << (*ii).first << ": " << (*ii).second << endl;  
  26.    }  
  27. }  

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

Below is the list of all member functions of map:

Constructor/Destructor

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

Iterators

FunctionsDescription
beginReturns an iterator pointing to the first element in the map.
cbeginReturns a const iterator pointing to the first element in the map.
endReturns an iterator pointing to the past-end.
cendReturns a constant iterator pointing to the past-end.
rbeginReturns a reverse iterator pointing to the end.
rendReturns a reverse iterator pointing to the beginning.
crbeginReturns a constant reverse iterator pointing to the end.
crendReturns a constant reverse iterator pointing to the beginning.

Capacity

FunctionsDescription
emptyReturns true if map is empty.
sizeReturns the number of elements in the map.
max_sizeReturns the maximum size of the map.

Element Access

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

Modifiers

FunctionsDescription
insertInsert element in the map.
eraseErase elements from the map.
swapExchange the content of the map.
clearDelete all the elements of the map.
emplaceConstruct and insert the new elements into the map.
emplace_hintConstruct and insert new elements into the map by hint.

Observers

FunctionsDescription
key_compReturn a copy of key comparison object.
value_compReturn a copy of value comparison object.

Operations

FunctionsDescription
findSearch for an element with given key.
countGets the number of elements matching with given key.
lower_boundReturns an iterator to lower bound.
upper_boundReturns an iterator to upper bound.
equal_rangeReturns the range of elements matches with given key.

Allocator

FunctionsDescription
get_allocatorReturns an allocator object that is used to construct the map.

Non-Member Overloaded Functions

FunctionsDescription
operator==Checks whether the two maps are equal or not.
operator!=Checks whether the two maps are equal or not.
operator<Checks whether the first map is less than other or not.
operator<=Checks whether the first map is less than or equal to other or not.
operator>Checks whether the first map is greater than other or not.
operator>=Checks whether the first map is greater than equal to other or not.
swap()Exchanges the element of two maps.

Comments

Popular Posts