Class map. Methods that provide information from the container
Contents
- 1. Method at(). Get the value by key
- 2. Methods begin(), end().Set iterator to start, end of array
- 3. Method count().Get number of duplicates by key
- 4. Method empty(). Determine if an array is empty
- 5. Method find(). Get iterator to given key
- 6. Method max_size(). Maximum container size
- 7. Method size(). Get the current number of elements in the array
- 8. Operator function operator[](). Get value by given key
- Related topics
Search other resources:
1. Method at(). Get the value by key
The at() method allows you to get a value based on a given key. The general form of a method declaration is as follows
inline const valueType& at(const keyType& key) const; inline valueType& at(const keyType& key);
here
- keyType – key type;
- valueType – value type;
- key – key value.
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method clear() - clear the array // 1. Form an associative array map<string, int> Days; Days.insert(make_pair("January", 31)); Days.insert(make_pair("February", 28)); Days.insert(make_pair("March", 31)); Days.insert(make_pair("April", 30)); // 2. Print the number of days in January - the at() method cout << Days.at("January") << endl; // 31 // 3. Get the number of days in April int daysApril = Days.at("April"); cout << daysApril << endl; // 30 // 4. Set a non-existent key - you need to process it with a try-catch block try { // the key "ABCD" is not in the container int t = Days.at("ABCD"); } catch (std::out_of_range e) { cout << e.what() << endl; } }
Result
31 30 invalid map<K, T> key
⇑
2. Methods begin(), end().Set iterator to start, end of array
The begin() and end() methods allow you to get an iterator to the beginning and end of an associative array. The end of an associative array is defined as the element following the last element of the array.
The general form of a method declaration is as follows:
iterator begin(); const iterator begin() const; iterator end(); const iterator end() const;
Figure 1 shows an example that shows the positions returned by the begin() and end() methods.
Figure 1. Returning a result from the begin() and end() methods
Example.
In the example, an associative container is formed, shown in Figure 1. Then, using the begin() and end() methods, the boundaries of the container are determined and the container is displayed on the screen using an iterator.
#include <iostream> #include <map> using namespace std; void main() { // Methods begin() and end() // 1. Create an associative container map<int, string> m1; m1.insert(pair<int, string>(1, "Winter")); m1.insert(pair<int, string>(2, "Spring")); m1.insert(pair<int, string>(3, "Summer")); m1.insert(pair<int, string>(4, "Autumn")); // 2. Get an iterator to the beginning and end of a container map<int, string>::iterator it, itEnd; it = m1.begin(); itEnd = m1.end(); // 3. Display the contents of the container on the screen cout << "m1:" << endl; while (it!=itEnd) { cout << it->first << " : " << it->second << endl; it++; } }
Result
m1: 1 : Winter 2 : Spring 3 : Summer 4 : Autumn
⇑
3. Method count().Get number of duplicates by key
The count() method returns the number of duplicates of an element with a key value. This method is relevant for the multimap class, in which several values can correspond to one key. The general form of a method declaration is as follows:
inline size_t count(const keyType &keyVal) const;
here
- keyType – key type;
- keyVal – value type.
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method count() - get the number of duplicates for a given key // 1. Form an associative array map<char, string> Colors; Colors.insert(make_pair('R', "Red")); Colors.insert(make_pair('G', "Green")); Colors.insert(make_pair('B', "Blue")); // 2. Attempt to create another duplicate of key 'B' Colors.insert(make_pair('B', "Black")); // 3. Print number of duplicates for key 'B' cout << Colors.count('B') << endl; // 1 // 4. Print number of duplicates for key 'R' cout << Colors.count('R') << endl; // 1 // 5. Attempting to access a non-existent key try { cout << Colors.count('F') << endl; // 0 } catch (out_of_range e) { cout << e.what() << endl; } }
Result
1 1 0
⇑
4. Method empty(). Determine if an array is empty
The empty() method allows you to determine if the associative container contains elements. The specification of the method is
inline bool empty() const;
The method returns true if the container has at least one element. Otherwise, the method returns false.
Example.
#include <iostream> #include <map> using namespace std; void main() { // The empty() method - determine if an array is empty // 1. Create an empty container map<int, char> m1; // 2. Call the empty() function if (m1.empty()) cout << "m1 is empty" << endl; else cout << "m1 is not empty" << endl; // 3. Add multiple elements to a container m1.insert(make_pair(1, 'A')); m1.insert(make_pair(2, 'B')); m1.insert(make_pair(3, 'C')); // 4. Re-call empty() if (m1.empty()) cout << "m1 is empty" << endl; else cout << "m1 is not empty" << endl; }
Result
m1 is empty m1 is not empty
⇑
5. Method find(). Get iterator to given key
The find() method returns an iterator that is set to the given key. The syntax for declaring the most common method implementations is:
inline iterator find(const keyType &keyVal) const; inline iterator find(const keyType &keyVal);
here
- keyType – the type of key;
- keyVal – the type of value.
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method find() - find element by key // 1. Form an associative array map<bool, string> m1; m1.insert(make_pair(true, "True")); m1.insert(make_pair(false, "False")); // 2. Find key true and print it map<bool, string>::iterator it; it = m1.find(true); cout << it->first << " : " << it->second << endl; // 3. Find key false and print it it = m1.find(false); cout << it->first << " : " << it->second << endl; }
Result
1 : True 0 : False
⇑
6. Method max_size(). Maximum container size
The max_size() method returns the maximum number of elements in the container. This number depends on the type of key and value in the pair, the size of free memory, etc.
Syntax of method declaration
inline size_t max_size() const;
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method max_size() - get the maximum possible container size // 1. Declare an empty container map<string, string> code; // 2. Add element to container code.insert(make_pair("USA", "1")); // 3. Display max_size() cout << code.max_size() << endl; // 164703072086692425 // 4. Add another element code.insert(make_pair("Ukraine", "380")); // 5. Display max_size() cout << code.max_size() << endl; // 164703072086692425 }
Result
164703072086692425 164703072086692425
⇑
7. Method size(). Get the current number of elements in the array
The size() method returns the number of elements in the associative container. When creating an empty container, the method will return 0.
Syntax of method declaration
inline size_t size() const;
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method size() - get the size of the container // 1. Declare an empty container map<string, string> code; // 2. Get the size of the container int size = code.size(); cout << size << endl; // 0 // 3. Add element to container code.insert(make_pair("USA", "1")); // 4. Display the size() cout << code.size() << endl; // 1 // 5. Add another element code.insert(make_pair("Ukraine", "380")); // 6. Display size() cout << code.size() << endl; // 2 }
Result
0 1 2
⇑
8. Operator function operator[](). Get value by given key
Using the operator function operator[](), you can access the values of an array by its keys in a natural way using square brackets []. The syntax for declaring a function in the map class is as follows
inline vType& map<kType, vType>::operator[](kType &&_KeyVal);
here
- kType – the type of key;
- vType – the type of value;
- _KeyVal – the value of a key.
The method can be used both in an expression and on the left side of the assignment operator =. If the _KeyVal key is not present in the container, and the [] operator is used on the left side of the assignment statement, a key is created with the corresponding value.
Example.
#include <iostream> #include <map> using namespace std; void main() { // Operator function operator[] - redefinition of operator [] // 1. Create container via initialization list initializer_list<pair<const string, int>> lst = { make_pair("Jan", 31), make_pair("Feb", 28), make_pair("Mar", 31), make_pair("Apr", 30), make_pair("May", 31), make_pair("Jun", 30), make_pair("Jul", 31), make_pair("Aug", 31), make_pair("Sep", 30), make_pair("Oct", 31), make_pair("Nov", 30), make_pair("Dec", 31) }; map<string, int> m1; m1.insert(lst); // 2. Get the number of days in March int d = m1["Mar"]; cout << "d = " << d << endl; // 3. Change the number of days in February m1["Feb"] = 29; // 4. Checking if the number of days in February has changed d = m1["Feb"]; // d = 29 cout << "d = " << d << endl; // 5. Create new pair "ABCD":333 m1["ABCD"] = 333; // 6. Display the array map<string, int>::iterator it = m1.begin(); cout << "m1:" << endl; while (it != m1.end()) { cout << it->first << ":" << it->second << endl; it++; } }
Result
d = 31 d = 29 m1: ABCD:333 Apr:30 Aug:31 Dec:31 Feb:29 Jan:31 Jul:31 Jun:30 Mar:31 May:31 Nov:30 Oct:31 Sep:30
⇑
Related topics
- Creating a container. Constructors. Creating pair. Overview of class functions and operators
- Methods that change data in the container: clear(), erase(), insert(), swap(), operator=()
⇑