VOOZH about

URL: https://www.geeksforgeeks.org/cpp/cpp-23-flat_map-header/

⇱ C++ 23 - <flat_map> Header - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C++ 23 - <flat_map> Header

Last Updated : 21 Apr, 2026

C++23 introduces a new header, <flat_map>, for storing key-value pairs in a more cache-friendly and efficient manner. It provides an alternative to traditional associative containers like std::map and std::unordered_map.

std::flat_map

std::flat_map is a container introduced in C++23 for storing key-value pairs. It combines features of std::map and std::vector:

  • Stores elements in sorted order based on keys
  • Uses contiguous memory layout for better cache performance
  • Provides efficient access and faster traversal

To use it, include the <flat_map> header and declare it with the required key and value types.

Syntax of std::flat_map

std::flat_map < keyType, valueType > name;

std::flat_map Initialization

We can initialize a flat_map container in C++ using the following methods:

1. Using the Initializer List

We can initialize std::flat_map by proving key and value pairs in the initializer list.

Example

std::flat_map<int, string> object_name { {3, "three"}, {1, "one"}, {4, "four"} };

2. Using the Range Constructor

We can use the range constructor to initialize the std::flat_map object with key and value pairs by specifying the range using the starting iterator and ending iterator.

Example

flat_map <keyType, valueType > object_name(startingIterator, endingIterator);

3. Using the Assignment Operator

We can simply initialize the flat_map object object with another already existing flat_map object using an assignment operator.

Example

std::flat_map <keyType, valueType > object_name;
object_name = prev_object;

4. Using the Move Assignment Operator

We can initialize a std::flat_map using the move assignment operator. This way of initializing a std::flat_map is memory efficient because the assigned object simply points to the existing memory location where the data is stored without creating unnecessary copies of data.

Example

std::flat_map <keyType, valueType > object_name;
object_name = move(prev_object);

Example


Output

Key: 1, Value: Geeks
Key: 2, Value: Geeks
Key: 3, Value: for

Commonly Used Methods

The following are some member functions for basic operation on the flat_map container:

1. Iterators

Iterators are used to traverse the container. The following function returns iterators to the flat_map containers:

  • begin() - Returns an iterator pointing to the first element in the flat_map.
  • end() - Returns an iterator pointing to the position just after the last element in the flat_map.

Example


Output

All key-value pairs in the map:
Key: 1, Value: Geeks
Key: 2, Value: for
Key: 3, Value: Geeks
Key: 4, Value: Coding

2. Modifiers

  • pair insert(keyvalue, mapvalue): Adds a new element to the map.
  • erase(iterator position): Removes the element at the position pointed by the iterator.
  • erase(const key_type& g): Removes the key value ā€˜g’ from the map.
  • clear() - Removes all the elements from the map.

Example


Output

Map before erasing:
Key: 1, Value: Geeks
Key: 2, Value: for
Key: 3, Value: Geeks
Key: 4, Value: Coding
Map after erasing by key:
Key: 1, Value: Geeks
Key: 2, Value: for
Key: 4, Value: Coding
The map is empty after clearing.

3. Size

  • size() - Returns the number of elements in the flat_map.
  • max_size() - Returns the maximum number of elements that the flat_map can hold.
  • empty() - Returns whether the flat_map is empty.

Example


Output

Size of the map: 3
Maximum possible size of the map: 256204778801521550
The map is not empty.

4. Map operations

  • bool contains(const key_type& g) const - Returns true if there is an element present with key value 'g' in the flat_map, else it returns false.
  • find(const key_type & g) - Returns an iterator pointing to an element with the key value 'g', or end() if such an element is not present in the flat_map.
  • count(const key_type & g) const - Returns the number of elements with key value 'g'.

Example


Output

Key 2 exists in the map.
Value at key 3: Geeks
Number of occurrences of key 2: 1

Advantages

  • One of the key benefits of flat_map is its ability to provide fast search, insertion, and deletion operations while maintaining the sorted order.
  • It offers logarithmic time complexity for these operations, making it appropriate for programs that require efficient key-value storage and retrieval.
  • The contiguous memory format of flat_map allows for cache-friendly access patterns, in addition to improving performance.
  • Used in dictionary applications to quickly map words to their definitions.
  • Useful in leaderboards to manage scores and retrieve or update rankings efficiently.
Comment
Article Tags:
Article Tags: