C++ STL: Maps

STD:: map


  • Containers that store the key value and mapped value. The key values are used to sort and uniquely identify the elements. The type of the key and the value may be different, one might be, for example, string and other an integer.
  • The data is always stored in sorted form, based on the type of key. If sorted data is not a must requirement then using a unorderd_map is good, they are faster than the map when accessing an individual element by their key.
  • The operator [key] can be used used to get the value.

Useful member functions

  • begin: Return an iterator to the beginning of the map.
  • end: Return an iterator to the end of the map.
  • empty: Return a bool, true if empty else false.
  • size: Return the size of the map.
  • insert: Insert elements.
  • erase: erase elements.
  • find: get an iterator to an element.
  • count: Count the elements with a specific key.
  • swap: swaps the content of two maps.
  • lower_bound: Returns an iterator to the first element in the container whose key is equivalent to the argument passed or greater than it.
  • upper_bound: Return an iterator to the first element in the container whose key is greater than the argument.
//basics of maps(similar is the situation for unordered maps)
//other member functions are similar to use and implement
//initialization
map<int, int> m;
///adding the data
m.insert(pair<int, int> (1, 3));
////accessing elements
m["1"]

Note: Here is the link to the official documentation for maps: http://www.cplusplus.com/reference/map/map/

No comments:

Post a Comment

Installing albert on ubuntu 19.04

Installing Albert on Ubuntu 19.04... Albert is not still released for ubuntu 19.04. But still, you can install it using the following ...