
- C++ Library - Home
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- The C++ STL Library
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- The C++ Advanced Library
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library -
The
The flat_map container acts as a wrapper to the two underlying containers, and gives a single approach by combining the advantages in both ordered and contiguous storage.
Including Header
To include the
#include
Functions of Header
Below is list of all functions from
Element Access
The element access functions provide mechanisms like retrieving or modifying elements in the container. This can be done using indices and keys.
S.NO | Functions & Description |
---|---|
1 |
at
This function accesses an element with bounds checking. |
2 |
operator[]
This function access or insert specified element. |
Accessing the Element
In the following example we are going to use, operator[] for accessing or insertion of an element.
#include#include int main() { std::flat_map myMap; myMap[1] = "One"; myMap[2] = "Two"; std::cout << "Element at key 1: " << myMap[1] << std::endl; return 0; }
Output
If we run the above code it will generate the following output
Element at key 1: One
Iterators
Iterators provide a way to traverse or manipulate the elements of a container. In flat_map, iterators are used to move from one key-value pair to another.
S.NO | Functions & Description |
---|---|
1 |
begin
These functions returns an iterator to the beginning of the container. |
2 |
end cend These functions returns an iterator to the end of the container. |
3 |
rbegin crbegin These function returns a reverse iterator to the beginning(last element). |
4 |
rend crend These functions returns a reverse iterator to the end(before the first element). |
Retrieving the Iterator
In the following example we are going to use, begin() to get an iterator at the beginning of the container.
#include#include int main() { std::flat_map myMap = {{1, "One"}, {2, "Two"}}; auto it = myMap.begin(); std::cout << "First element: " << it->t << " -> " << it->second << std::endl; return 0; }
Output
If we run the above code it will generate the following output
First element: 1 -> One
Capacity
The capacity functions are used to check and provide information about the containers size and capacity.
S.NO | Functions & Description |
---|---|
1 |
empty
This function checks whether the container adaptor is empty. |
2 |
size
This function returns the number of elements. |
3 |
max_size
This function returns the maximum possible number of elements. |
Checking Existence of Container
In the following example we are going to use, empty() to chech whether the container is empty.
#include#include int main() { std::flat_map myMap; if (myMap.empty()) { std::cout << "The flat_map is empty." << std::endl; } else { std::cout << "The flat_map is not empty." << std::endl; } return 0; }
Output
If we run the above code it will generate the following output
The flat_map is empty.
Modifiers
Modifier Functions change the contents of the container by inserting, replacing, or deleting elements.
S.NO | Functions & Description |
---|---|
1 |
emplace
This function constructs element in-place. |
2 |
emplace_hint
This function constructs elements in-place using a hint for insertion position. |
3 |
try_emplace
This function inserts in-place if the key does not exist, does nothing if the key exists. |
4 |
insert_or_assign
This function inserts an element or assigns to the current element if the key already exists. |
5 |
extract
This function extracts the underlying containers. |
6 |
replace
This function replaces the underlying containers. |
7 |
erase
This function erases elements. |
8 |
erase_if
This function erases all elements satisfying specific criteria. |
9 |
operator=
This function assigns values to the container adaptor. |
Inserting an Element In-Place
In the following example we are going to use, emplace() to insert an element in a specific place.
#include#include int main() { std::flat_map myMap; myMap.emplace(1, "One"); std::cout << "Element emplaced: " << myMap[1] << std::endl; return 0; }
Output
If we run the above code it will generate the following output
Element emplaced: One
Lookup
S.NO | Functions & Description |
---|---|
1 |
find
This function finds element with specific key. |
2 |
count
This function returns the number of elements matching specific key. |
3 |
contains
This function checks if the container contains element with specific key. |
4 |
lower_bound
This function returns an iterator to the first element not less than the given key. |
5 |
upper_bound
This function returns an iterator to the first element greater than the given key. |
6 |
equal_range
This function returns range of elements matching a specific key. |
Finding an Element Using Key
In the following example we are going to use, find() to find an element by key.
#include#include int main() { std::flat_map myMap = {{1, "One"}, {2, "Two"}}; auto it = myMap.find(1); if (it != myMap.end()) { std::cout << "Found element: " << it->first << " -> " << it->second << std::endl; } else { std::cout << "Element not found." << std::endl; } return 0; }
Output
If we run the above code it will generate the following output
Found element: 1 -> One
Observers
The observer functions provide information about how the container operates, particularly regarding key comparison and access to underlying data structures.
S.NO | Functions & Description |
---|---|
1 |
key_comp
This function is used to compares keys. |
2 |
value_comp
Returns the function that compares keys in objects of type value_type. |
3 |
keys
This function provide an direct access to the container of underlying values. |
4 |
values
This function provides an direct access to the container of underlying values. |
Comparing the Keys
In the following example we are going to use, key_comp() to compare the keys.
#include#include int main() { std::flat_map myMap = {{1, "One"}, {2, "Two"}}; auto comp = myMap.key_comp(); if (comp(1, 2)) { std::cout << "Key 1 is less than Key 2." << std::endl; } else { std::cout << "Key 1 is not less than Key 2." << std::endl; } return 0; }
Output
If we run the above code it will generate the following output
Key 1 is less than Key 2.