
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <flat_map>
The <flat_map> header is part of the containers library, provides various functions as a sorted associative container for low memory usage, fast access, and store the key-value pairs with unique keys.
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 <flat_map> Header
To include the <flat_map> header in your C++ program, you can use the following syntax.
#include <flat_map>
Functions of <flat_map> Header
Below is list of all functions from <flat_map> header.
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 <iostream> #include <flat_map> int main() { std::flat_map<int, std::string> 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 <iostream> #include <flat_map> int main() { std::flat_map<int, std::string> 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 <iostream> #include <flat_map> int main() { std::flat_map<int, std::string> 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 <iostream> #include <flat_map> int main() { std::flat_map<int, std::string> 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 <iostream> #include <flat_map> int main() { std::flat_map<int, std::string> 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 <iostream> #include <flat_map> int main() { std::flat_map<int, std::string> 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.