Class map. Associative container. Creating a container. Constructors. Creating pair. Overview of class functions and operators
Contents
- 1. General information about the map class. Class declaration
- 2. Constructors of map class. Create an associative container
- 3. Functions of map class. Review
- 4. Comparison operators of the map class. Review. Examples
- 5. Structure pair<Key, Value>
- Related topics
Search other resources:
1. General information about the map class. Class declaration
The map class is an associative container or hash table. An associative map container is a set of elements, each of which is represented as key:value pairs. The key is a name with which you can get the desired value. In other words, the value is accessed using the key. A relationship (association) is established between the key and the value.
All keys in an associative map container are unique (keys cannot be duplicated). If it is necessary to ensure key repeatability, then another multimap class is used for this.
To use the capabilities of the map class, you need to include the <map> header
#include <map>
According to the documentation of the map class template declaration, the following
template <class Key, class T, class Comp = less<Key>, class Allocator = allocator<pair<const key, T> class map
here
- Key – a class that defines the type of key;
- T – type of data (values) obtained by a key of type Key;
- Comp is a function that allows you to compare two keys. By default, the Comp() function uses the standard less() functor;
- Allocator is a class that acts as a memory allocator. By default, the usual allocator class is used.
⇑
2. Constructors of map class. Create an associative container
The map class has several constructors. The constructor that creates an empty array looks like this
explicit map(const Comp &compare_func = Comp(), const Allocator &alloc = Allocator());
A constructor that creates an associative container based on obj, which is also of type map
map(const map<Key, T, Comp, Allocator> &obj);
Constructor that creates an associative array containing the elements of a given range
template <class InIterator> list(InIterator start, InIterator end, const Comp &compare_func = Comp(), const Allocator &a = Allocator());
All three of the above constructors use the following notation:
- start, end – iterators specifying the beginning and end of the range;
- compare_func – a function that determines the order of the array elements.
Example.
The example creates an associative array of pairs in which the key is of type int and the value is of type string. To create an array, different methods (constructors) are used.
#include <iostream> #include <map> using namespace std; void main() { // Constructors of class map. // Constructor 1. Create an empty array of int:string pairs map<int, string> m1; m1.insert(pair<int, string>(1, "One")); // add pair 1:"One" m1.insert(pair<int, string>(2, "Two")); m1.insert(pair<int, string>(4, "Four")); m1.insert(pair<int, string>(5, "Five")); // Constructor 2. Create another array based on the existing one map<int, string> m2(m1); cout << m2.at(2) << endl; // Two cout << m2.at(4) << endl; // Four // Constructor 3. Create an array based on iterators // Take the range of positions [0; 1] inclusive (positions are numbered from 0) // 3.1. Declare iterators map<int, string>::iterator it_start; map<int, string>::iterator it_end; // 3.2. Set the iterator it_start to the first element of the array m1. it_start = m1.begin(); // 3.3. Set the iterator it_end beyond the element at index 1 it_end = m1.begin(); // 3.4. Rewind the iterator by 2 positions (for position with index 1) for (int i = 0; i < 2; i++) it_end++; // 3.5. Create array m3 based on array m1 map<int, string> m3(it_start, it_end); // 3.6. Display the array m3 map<int, string>::iterator it = m3.begin(); cout << "Array m3:" << endl; while (it != m3.end()) { cout << it->first << " : " << it->second << endl; it++; } cout << "--------------------" << endl; }
Result
Two Four Array m3: 1 : One 2 : Two --------------------
⇑
3. Functions of map class. Review
The map class has the following main functions:
- at() – returns a value by a known key;
- begin() – returns an iterator to the first element of the container;
- clear() – removes all elements from the array;
- count() – returns the number of duplicate elements for the given key;
- empty() – determines whether the associative array is empty (does not contain any element);
- end() – returns an iterator set by the last element of the container;
- erase() – removes an element from an associative container;
- find() – returns an iterator set to the given key;
- insert() – adds a new pair to the container;
- max_size() – returns the size actually allocated for the associative container, taking into account its further growth;
- size() – returns the current number of elements (size) in the associative container;
- swap() – swaps the contents of two containers;
- operator=() – assigns one container to another;
- operator[]() – returns the value by the given key.
⇑
4. Comparison operators of the map class. Review. Examples
The map class defines comparison operators: ==, !=, <=, >=, <, >.
The equality comparison operators == and inequality != compare both keys and values. If at least one mismatch is found in key:value pairs or the number of elements in containers is different when using the == comparison operator, then the result will be false. The != operator is the opposite of the == operator, so in this case, the result of this operator will be true.
In the <, >, <=, >= operators, the comparison occurs in the following order:
- First, the container keys are compared in order from first to last. This means that the key of the first container is compared with the key of the second container. The first mismatch in the keys will set the appropriate result.
- If the corresponding container keys match, the next values are compared in order from first to last. The values are sequentially scanned and compared.
- If both keys and values match up to some pair, then the lengths of containers are compared.
Example 1. Comparison for equality and inequality.
... // Comparison operators // 1. Comparison of identical associative containers map<int, string> m1; m1.insert(make_pair(5, "Five")); map<int, string> m2; m2.insert(make_pair(5, "Five")); bool res = m1 == m2; // res = true res = m1 != m2; // res = false // 2. Comparison of containers in which the value differs map<int, string> m3, m4; m3.insert(make_pair(4, "Four")); m4.insert(make_pair(4, "4")); res = m3 == m4; // res = false res = m3 != m4; // res = true // 3. Comparison of containers in which the keys differ map<int, string> m5, m6; m5.insert(make_pair(6, "Six")); m6.insert(make_pair(7, "Six")); res = m3 == m4; // res = false res = m3 != m4; // res = true cout << res << endl; ...
Example 2. More/less comparison.
... // Comparison operators // 1. Comparison >, >= containers are equal map<int, string> m1; m1.insert(make_pair(5, "Five")); map<int, string> m2; m2.insert(make_pair(5, "Five")); bool res = m1 > m2; // res = false res = m1 >= m2; // res = true // 2. Comparison >, >=, <, <= map<int, string> m3, m4; m3.insert(make_pair(1, "One")); m4.insert(make_pair(0, "Zero")); res = m3 > m4; // res = true res = m3 >= m4; // res = true res = m3 < m4; // res = false res = m3 <= m4; // res = false // 3. Keys are equal, values are different // keys are compared first, then values map<int, string> m5, m6; m5.insert(make_pair(1, "AAA")); m6.insert(make_pair(1, "ABC")); res = m5 > m6; // res = false res = m5 >= m6; // res = false res = m5 < m6; // res = true res = m5 <= m6; // res = true // 4. Container lengths are different, // first key:value pairs match map<int, string> m7, m8; m7.insert(make_pair(1, "One")); m7.insert(make_pair(2, "Two")); m8.insert(make_pair(1, "One")); res = m7 > m8; // res = true - take into account the length of the containers res = m7 >= m8; // res = true res = m7 < m8; // res = false res = m7 <= m8; // res = false // 5. The length of the first container is greater than the length of the second, // however, the first key of the first container is less than the first key of the second. map<int, string> m9, m10; m9.insert(make_pair(1, "One")); m9.insert(make_pair(2, "Two")); m10.insert(make_pair(2, "Two")); res = m9 > m10; // res = false res = m9 >= m10; // res = false res = m9 < m10; // res = true res = m9 <= m10; // res = true ...
⇑
5. Structure pair<Key, Value>
To create a key:value pair, you need to use the pair structure, which has the following template declaration
template <class Ktype, class Vtype> struct pair { typedef Ktype first_type; typedef Vtype second_type; Ktype first; Vtype second; }
here
- Ktype – type of key;
- Vtype – value type;
- first – key;
- second – value.
Once created, a pair of type pair<Key, Value> can be added to the associative container using the insert() method.
An instance of the pair struct type is created in a variety of ways using the appropriate constructors. The following are some basic ways to create a key:value pair.
⇑
5.1. Creating a key:value pair. Using the structure first, second
The pair<Key, Value> structure has fields that give direct access to the key and value:
- first – defines the key;
- second – defines the value.
In the most general case, creating a pair with filling in the first, second fields is as follows
// Creating a pair object pair<K, V> p; // Filling in values p.first = key; p.second = value;
here
- K, V – key type and value type;
- key – value of a key of type K;
- value – value associated with key K.
Example.
... // Using the first, second fields of the pair<K, V> structure // 1. Create a pair of types double, float and display it on the screen pair<double, float> p; p.first = 2.5; p.second = 100.55f; cout << p.first << " : " << p.second << endl; // 2. Create a char:string type pair and print it to the screen pair<char, string> season; season.first = 'W'; season.second = "Winter"; cout << p.first << " : " << p.second << endl; ...
⇑
5.2. Creating a key:value pair with a constructor
An instance of the pair<Key, Value> structure can be created using the appropriate constructor of the following form
pair<K, V> objP(key, value);
here
- K, V – key type and value type;
- key, value – specific key and value values.
Example.
... // Creating a pair using the pair(K, V) constructor // 1. Create a pair of types char, int pair<char, int> p1('H', 16); cout << p1.first << " : " << p1.second << endl; // 'H' : 16 // 2. Create a pair of types bool, string pair<bool, string> p2(true, "True"); cout << p2.first << " : " << p2.second << endl; // 1 : True ...
⇑
5.3. Creating a key:value pair based on another pair
If you need to get an instance of the pair<Key, Value> structure from another pair, then the following form is used
pair<K, V> p2(p1);
here
- p1 is the original instance. The first, second values of this instance are copied to the p2 instance;
- p2 is a copy instance. The key (first) and value (second) of p2 are populated from the p1 instance.
Example.
... // Creating a pair with another pair // 1. Create a pair of types char, int pair<char, int> p1('H', 16); // 2. Get another pair pair<char, int> p2(p1); cout << p2.first << " : " << p2.second << endl; // H : 16 ...
⇑
5.4. Creating a pair using the make_pair() function
A pair of type pair<Key, Value> can be created using the make_pair() function, which, according to the documentation, has the following prototype
template <class Ktype, class Vtype> pair<Ktype, Vtype> make_pair(const Ktype &k, const Vtype &v);
here
- Ktype – the type of key;
- Vtype – the type of value.
The convenience of using the make_pair() method is that the types of objects (key and pair) are determined automatically by the compiler.
Example.
... // Creating a pair using the make_pair() method // 1. Create a pair of type int:string pair<int, string> p1; p1 = make_pair(2, "Two"); // 2. Create a char:int pair and add it to the associative container pair<char, int> p2 = make_pair('B', 2); map<char, int> m1; m1.insert(p2); // 3. Create a pair in which the type is determined automatically and add it to the container m1.insert(make_pair('O', 8)); ...
⇑
Related topics
- Class map. Methods that provide information from the container: at(), begin(), end(), count(), empty(), find(), size(), max_size(), operator[]()
- Class map. Methods that change data in the container: clear(), erase(), insert(), swap(), operator=()
⇑