Maps are used to replicate associative arrays. Maps contain sorted key-value pair, in which each key is unique and cannot be changed, and it can be inserted or deleted but cannot be altered. Value associated with keys can be altered. We can search, remove and insert in a map within O(n) time complexity. Keys are arranged in ascending order, its because maps always arrange its keys in sorted order. In case the keys are of string type, they are sorted lexicographically.
Syntax
map<key_type , value_type> map_name;
This will create a map with key of type Key_type and value of type value_type. One thing which is to remembered 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.
Member Functions
- mapped_type& at (const key_type& k);
const mapped_type& at (const key_type& k) const;
Returns a reference to the mapped value of the element identified with key k. - bool empty() const noexcept;
Returns whether the map container is empty (i.e. whether its size is 0). - (1) iterator erase (const_iterator position);
(2) size_type erase (const key_type& k);
(3) iterator erase (const_iterator first, const_iterator last);
Removes from the map container either a single element or a range of elements ([first,last)). - size_type size() const noexcept;
Returns the number of elements in the map container. - (1) iterator find (const key_type& k);
(2) const_iterator find (const key_type& k) const;
Searches the container for an element with a key equivalent to k and returns an iterator to it if found, otherwise it returns an iterator to map::end. - (1) pair<iterator,bool> insert (const value_type& val);
template <class P> pair<iterator,bool> insert (P&& val);
(2) iterator insert (const_iterator position, const value_type& val);
template <class P> iterator insert (const_iterator position, P&& val);
(3)template <class InputIterator>
void insert (InputIterator first, InputIterator last);
(4) void insert (initializer_list<value_type> il);
Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted. - void clear() noexcept;
Removes all elements from the map container (which are destroyed), leaving the container with a size of 0. - iterator begin() noexcept;
const_iterator begin() const noexcept;
Returns an iterator referring to the first element in the map container. - iterator end() noexcept;
const_iterator end() const noexcept;
Returns an iterator referring to the past-the-end element in the map container. - iterator lower_bound (const key_type& k);
const_iterator lower_bound (const key_type& k) const;
Returns an iterator pointing to the first element in the container whose key is not considered to go before k (i.e., either it is equivalent or goes after). - iterator upper_bound (const key_type& k);
const_iterator upper_bound (const key_type& k) const;
Returns an iterator pointing to the first element in the container whose key is considered to go after k.
Example
#include <iostream> #include <map> using namespace std; int main () { // Creates a map m with keys 1,2,3 map<int,int> m{{1,2}, {2,3}, {3,4}}; // Creates a map with keys of type character and values of type integer map<string,int> map1; map1["abc"]=100; map1["b"]=200; map1["c"]=300; map1["def"]=400; // creates a map map2 which have entries copied from map1.begin() to map1.end() map<char,int> map2 (map1.begin(), map1.end()); // creates map map3 which is a copy of map m map<char,int> map3 (m); // Prints value associated with key 1 cout << m.at(1) ; // Prints value associated with key 3 cout << m[3] ; // changes the value associated with key 2 m[2] = 100; while (!m.empty()) { cout << m.begin()->first << " => " << m.begin()->second << '\n'; m.erase(m.begin()); } // inserts a new entry of key = 4 and value = 5 m.insert( pair<int,int> (4,5)); // Points to entry having key =2 map::iterator i = m.find(2); // Insert with hint position m.insert (i, std::pair<int,int>(45,300)); return 0 }