The C++ Standard Library
- C++ Library - <valarray>
- C++ Library - <utility>
- C++ Library - <typeinfo>
- C++ Library - <tuple>
- C++ Library - <thread>
- C++ Library - <string>
- C++ Library - <stdexcept>
- C++ Library - <regex>
- C++ Library - <numeric>
- C++ Library - <new>
- C++ Library - <memory>
- C++ Library - <locale>
- C++ Library - <limits>
- C++ Library - <functional>
- C++ Library - <exception>
- C++ Library - <complex>
- C++ Library - <atomic>
- C++ Library - <streambuf>
- C++ Library - <sstream>
- C++ Library - <ostream>
- C++ Library - <istream>
- C++ Library - <iostream>
- C++ Library - <iosfwd>
- C++ Library - <ios>
- C++ Library - <iomanip>
- C++ Library - <fstream>
- C++ Library - Home
The C++ STL Library
- C++ Library - <iterator>
- C++ Library - <algorithm>
- C++ Library - <vector>
- C++ Library - <unordered_set>
- C++ Library - <unordered_map>
- C++ Library - <stack>
- C++ Library - <set>
- C++ Library - <queue>
- C++ Library - <map>
- C++ Library - <list>
- C++ Library - <forward_list>
- C++ Library - <deque>
- C++ Library - <bitset>
- C++ Library - <array>
C++ Programming Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
C++ Library - <map>
Introduction to map
Map is dictionary pke data structure. It is a sequence of (key, value) pair, where only single value is associated with each unique key. It is often referred as associative array.
In map key values generally used to sort the elements. For map data type of key and value can differ and it is represented as
typedef pair<const Key, T> value_type;
Maps are typically implemented as Binary Search Tree.
Zero sized maps are also vapd. In that case map.begin() and map.end() points to same location.
Definition
Below is definition of std::map from <map> header file
template < class Key, class T, class Compare = less<Key>, class Alloc = allocator<pair<const Key,T> > > class map;
Parameters
Key − Type of the key.
T − Type of the mapped values.
Compare − A binary predicate that takes two element keys as arguments and returns a bool.
Alloc − Type of the allocator object.
T may be substituted by any other data type including user-defined type.
Member types
Following member types can be used as parameters or return type by member functions.
Sr.No. | Member types | Definition |
---|---|---|
1 | key_type | Key (First parameter of the template) |
2 | mapped_type | T (Second parameter of the template) |
3 | key_compare | Compare (Third parameter of the template) |
4 | allocator_type | Alloc (Fourth parameter of the template) |
5 | value_type | pair<const key_type,mapped_type> |
6 | value_compare | Nested function class to compare elements |
7 | reference | allocator_type::reference |
8 | const_reference | allocator_type::const_reference |
9 | pointer | allocator_type::pointer |
10 | const_pointer | allocator_type::const_pointer |
11 | iterator | bi-directional iterator to value_type |
12 | const_iterator | bi-directional iterator to const value_type |
13 | reverse_iterator | reverse iterator |
14 | const_reverse_iterator | constant reverse iterator |
15 | difference_type | ptrdiff_t |
16 | size_type | size_t |
Functions from <map>
Below is pst of all methods from <map> header.
Constructors
Sr.No. | Method & Description |
---|---|
1 | Constructs an empty map with zero elements. |
default constructor
2 | Constructs a map with as many elements as in range of first to last. |
range constructor
3 | Constructs a map with copy of each elements present in existing map. |
copy constructor
4 | Constructs a map with the contents of other using move semantics. |
move constructor
5 | Constructs a map from initiapze pst. |
initiapzer pst constructor
Destructor
Sr.No. | Method & Description |
---|---|
1 | Destroys map object by deallocating it s memory. |
Member functions
Sr.No. | Method & Description |
---|---|
1 | Returns a reference to the mapped value associated with key k. |
2 | Returns a iterator which refers to the first element of the map. |
3 | Returns a constant iterator which refers to the first element of the map. |
4 | Returns a constant iterator which points to past-the-end element of the map. |
5 | Destroys the map by removing all elements and sets size of map to zero. |
6 | Returns number of mapped values associated with key k. |
7 | Returns a constant reverse iterator which points to the last element of the container i. |
8 | Returns a constant reverse iterator which points to the theoretical element preceding the first element in the container i. |
9 | Extends container by inserting new element. |
10 | Inserts a new element in a map using hint as a position for element. |
hint version
11 | Tests whether map is empty or not. |
12 | Returns an iterator which points to past-the-end element in the map. |
13 | Returns range of elements that matches specific key. |
14 | Removes single element of the map from position. |
position version
15 | Removes single element of the map from position. |
position version
16 | Removes mapped value associated with key k. |
key
17 | Removes range of element from the the map. |
range version
18 | Removes range of element from the the map. |
range version
19 | Finds an element associated with key k. |
20 | Returns an allocator associated with map. |
21 | Extends container by inserting new element in map. |
single element
22 | Extends container by inserting new element in map. |
hint version
23 | Extends container by inserting new elements in the map. |
range version
24 | Extends map by inserting new element. |
move hint verstion
25 | Extends map by inserting new element from initiapzer pst. |
initiapzer pst version
26 | Returns a function object that compares the keys, which is a copy of this container s constructor argument comp. |
27 | Returns an iterator pointing to the first element which is not less than key k. |
28 | Returns the maximum number of elements can be held by map. |
29 | Assign new contents to the map by replacing old ones and modifies size if necessary. |
copy version
30 | Move the contents of one map into another and modifies size if necessary. |
move version
31 | Copy elements from initiapzer pst to map. |
initiapzer pst version
32 | If key k matches an element in the container, then method returns a reference to the element. |
copy version
33 | If key k matches an element in the container, then method returns a reference to the element. |
move version
34 | Returns a reverse iterator which points to the last element of the map. |
35 | Returns a reverse iterator which points to the reverse end of the map i. |
36 | Returns the number of elements present in the map. |
37 | Exchanges the content of map with contents of map x. |
38 | Returns an iterator pointing to the first element which is greater than key k. |
39 | Returns a function object that compares objects of type std::map::value_type. |
Non-member overloaded functions
Sr.No. | Method & Description |
---|---|
1 | Tests whether two maps are equal or not. |
2 | Tests whether two maps are equal or not. |
3 | Tests whether first map is less than other or not. |
4 | Tests whether first map is less than or equal to other or not. |
5 | Tests whether first map is greater than other or not. |
6 | Tests whether first map is greater than or equal to other or not. |
7 | Exchanges the content of map with contents of map x. |
Introduction to multimap
Multimap is dictionary pke data structure. It is a sequence of (key, value) pair, where multiple values can be associate with equivalent keys. It is often referred as associative array.
In multimap key values generally used to sort the elements. For multimap data type of key and value can differ and it is represented as
typedef pair<const Key, T> value_type;
Multimaps are typically implemented as Binary Search Tree.
Zero sized multimaps are also vapd. In that case multimap.begin() and multimap.end() points to same location.
Definition
Below is definition of std::multimap from <multimap> header file
template < class Key, class T, class Compare = less<Key>, class Alloc = allocator<pair<const Key,T> > > class multimap;
Parameters
Key − Type of the key.
T − Type of the mapped values.
Compare − A binary predicate that takes two element keys as arguments and returns a bool.
Alloc − Type of the allocator object.
T may be substituted by any other data type including user-defined type.
Member types
Following member types can be used as parameters or return type by member functions.
Sr.No. | Member types | Definition |
---|---|---|
1 | key_type | Key (First parameter of the template) |
2 | mapped_type | T (Second parameter of the template) |
3 | key_compare | Compare (Third parameter of the template) |
4 | allocator_type | Alloc (Fourth parameter of the template) |
5 | value_type | pair<const key_type,mapped_type> |
6 | value_compare | Nested function class to compare elements |
7 | reference | allocator_type::reference |
8 | const_reference | allocator_type::const_reference |
9 | pointer | allocator_type::pointer |
10 | const_pointer | allocator_type::const_pointer |
11 | iterator | bi-directional iterator to value_type |
12 | const_iterator | bi-directional iterator to const value_type |
13 | reverse_iterator | reverse iterator |
14 | const_reverse_iterator | constant reverse iterator |
15 | difference_type | ptrdiff_t |
16 | size_type | size_t |
Functions from <multimap>
Below is pst of all methods from <multimap> header.
Constructors
Sr.No. | Method & Description |
---|---|
1 | Constructs an empty multimap with zero elements. |
default constructor
2 | Constructs a multimap with as many elements as in range of first to last. |
range constructor
3 | Constructs a multimap with copy of each elements present in existing multimap. |
copy constructor
4 | Constructs a multimap with the contents of other using move semantics. |
move constructor
5 | Constructs a multimap from initiapze pst. |
initiapzer pst constructor
Destructor
Sr.No. | Method & Description |
---|---|
1 | Destroys multimap object by deallocating it s memory. |
Member functions
Sr.No. | Method & Description |
---|---|
1 | Returns a iterator which refers to the first element of the multimap. |
2 | Returns a constant iterator which refers to the first element of the multimap. |
3 | Returns a constant iterator which points to past-the-end element of the multimap. |
4 | Destroys the multimap by removing all elements and sets size of multimap to zero. |
5 | Returns number of multimapped values associated with key k. |
6 | Returns a constant reverse iterator which points to the last element of the container. |
7 | Returns a constant reverse iterator which points to the theoretical element preceding the first element in the container. |
8 | Extends container by inserting new element. |
9 | Inserts a new element in a multimap using hint as a position for element. |
hint version
10 | Tests whether multimap is empty or not. |
11 | Returns an iterator which points to past-the-end element in the multimap. |
12 | Returns range of elements that matches specific key. |
13 | Removes single element of the multimap from position. |
position version
14 | Removes single element of the multimap from position. |
position version
15 | Removes mapped value associated with key k. |
key
16 | Removes range of element from the the multimap. |
range version
17 | Removes range of element from the the multimap. |
range version
18 | Finds an element associated with key k. |
19 | Returns an allocator associated with multimap. |
20 | Extends container by inserting new element in multimap. |
single element
21 | Extends container by inserting new element in multimap. |
hint version
22 | Extends container by inserting new elements in the multimap. |
range version
23 | Extends multimap by inserting new element. |
move hint verstion
24 | Extends multimap by inserting new element from initiapzer pst. |
initiapzer pst version
25 | Returns a function object that compares the keys, which is a copy of this container s constructor argument comp. |
26 | Returns an iterator pointing to the first element which is not less than key k. |
27 | Returns the maximum number of elements can be held by multimap. |
28 | Assign new contents to the multimap by replacing old ones and modifies size if necessary. |
copy version
29 | Move the contents of one multimap into another and modifies size if necessary. |
move version
30 | Copy elements from initiapzer pst to multimap. |
initiapzer pst version
31 | Returns a reverse iterator which points to the last element of the multimap. |
32 | Returns a reverse iterator which points to the reverse end of the multimap. |
33 | Returns the number of elements present in the multimap. |
34 | Exchanges the content of multimap with contents of multimap x. |
35 | Returns an iterator pointing to the first element which is greater than key k. |
36 | Returns a function object that compares objects of type std::multimap::value_type. |
Non-member overloaded functions
Sr.No. | Method & Description |
---|---|
1 | Tests whether two multimaps are equal or not. |
2 | Tests whether two multimaps are equal or not. |
3 | Tests whether first multimap is less than other or not. |
4 | Tests whether first multimap is less than or equal to other or not. |
5 | Tests whether first multimap is greater than other or not. |
6 | Tests whether first multimap is greater than or equal to other or not. |
7 | Exchanges the content of multimap with contents of multimap x. |