Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/flat multimap"

From cppreference.com
< cpp‎ | container
m (fmt.)
m (Minor typo fix.)
 
Line 24: Line 24:
 
* the complexity of insertion and erasure operations is linear.
 
* the complexity of insertion and erasure operations is linear.
  
A flat multimap supports most {{named req|AssociativeContainer}}'s operations that use equal keys.
+
A flat multimap supports most of {{named req|AssociativeContainer}}'s operations that use equal keys.
  
 
===Iterator invalidation===
 
===Iterator invalidation===

Latest revision as of 21:30, 8 November 2024

 
 
 
 
Defined in header <flat_map>
template<

    class Key,
    class T,
    class Compare = std::less<Key>,
    class KeyContainer = std::vector<Key>,
    class MappedContainer = std::vector<T>

> class flat_multimap;
(since C++23)

The flat multimap is a container adaptor that gives the functionality of an associative container that contains key-value pairs, while permitting multiple entries with the same key value. Keys are sorted by using the comparison function Compare.

The class template flat_multimap acts as a wrapper to the two underlying containers, passed as objects of type KeyContainer and MappedContainer respectively. The first container is sorted, and for each key its corresponding value is in the second container at the same index (offset). The number of elements in both containers is the same.

Everywhere the standard library uses the Compare requirements, uniqueness is determined by using the equivalence relation. Informally, two objects a and b are considered equivalent if neither compares less than the other: !comp(a, b) && !comp(b, a).


std::flat_multimap meets the requirements of Container, ReversibleContainer, optional container requirements, and all requirements of AssociativeContainer (including logarithmic search complexity), except that:

  • requirements related to nodes are not applicable,
  • iterator invalidation requirements differ,
  • the complexity of insertion and erasure operations is linear.

A flat multimap supports most of AssociativeContainer's operations that use equal keys.

Contents

[edit] Iterator invalidation

[edit] Template parameters

Key - The type of the keys. The program is ill-formed if Key is not the same type as KeyContainer::value_type.
T - The type of mapped values. The program is ill-formed if T is not the same type as MappedContainer::value_type.
Compare - A Compare type providing a strict weak ordering.
KeyContainer
MappedContainer
- The types of the underlying SequenceContainer to store keys and mapped values correspondingly. The iterators of such containers should satisfy LegacyRandomAccessIterator or model random_access_iterator. Invocations of their member functions size and max_size should not exit via an exception.

The standard containers std::vector and std::deque satisfy these requirements.

[edit] Member types

Type Definition
key_container_type KeyContainer[edit]
mapped_container_type MappedContainer[edit]
key_type Key[edit]
mapped_type T[edit]
value_type std::pair<key_type, mapped_type>[edit]
key_compare Compare[edit]
reference std::pair<const key_type&, mapped_type&>[edit]
const_reference std::pair<const key_type&, const mapped_type&>[edit]
size_type std::size_t[edit]
difference_type std::ptrdiff_t[edit]
iterator implementation-defined LegacyInputIterator and random_access_iterator to value_type[edit]
const_iterator implementation-defined LegacyInputIterator and random_access_iterator to const value_type[edit]
reverse_iterator std::reverse_iterator<iterator>[edit]
const_reverse_iterator std::reverse_iterator<const_iterator>[edit]
containers type describing the underlying containers

struct containers
{
    key_container_type keys;
    mapped_container_type values;
};
[edit]

[edit] Member classes

compares objects of type value_type
(class) [edit]

[edit] Member objects

Member Description
containers c (private) the adapted containers
(exposition-only member object*)
key_compare compare (private) the comparison function object
(exposition-only member object*)

[edit] Member functions

constructs the flat_multimap
(public member function) [edit]
(destructor)
(implicitly declared)
destroys every element of the container adaptor
(public member function)
assigns values to the container adaptor
(public member function) [edit]
Iterators
returns an iterator to the beginning
(public member function) [edit]
returns an iterator to the end
(public member function) [edit]
returns a reverse iterator to the beginning
(public member function) [edit]
returns a reverse iterator to the end
(public member function) [edit]
Capacity
checks whether the container adaptor is empty
(public member function) [edit]
returns the number of elements
(public member function) [edit]
returns the maximum possible number of elements
(public member function) [edit]
Modifiers
constructs element in-place
(public member function) [edit]
constructs elements in-place using a hint
(public member function) [edit]
inserts elements
(public member function) [edit]
inserts a range of elements
(public member function) [edit]
extracts the underlying containers
(public member function) [edit]
replaces the underlying containers
(public member function) [edit]
erases elements
(public member function) [edit]
swaps the contents
(public member function) [edit]
clears the contents
(public member function) [edit]
Lookup
finds element with specific key
(public member function) [edit]
returns the number of elements matching specific key
(public member function) [edit]
checks if the container contains element with specific key
(public member function) [edit]
returns an iterator to the first element not less than the given key
(public member function) [edit]
returns an iterator to the first element greater than the given key
(public member function) [edit]
returns range of elements matching a specific key
(public member function) [edit]
Observers
returns the function that compares keys
(public member function) [edit]
returns the function that compares keys in objects of type value_type
(public member function) [edit]
direct access to the underlying keys container
(public member function) [edit]
direct access to the underlying values container
(public member function) [edit]

[edit] Non-member functions

lexicographically compares the values of two flat_multimaps
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
erases all elements satisfying specific criteria
(function template) [edit]

[edit] Helper classes

specializes the std::uses_allocator type trait
(class template specialization) [edit]

[edit] Tags

indicates that elements of a range are sorted (uniqueness is not required)
(tag)[edit]

[edit] Deduction guides

[edit] Notes

The member types iterator and const_iterator may be aliases to the same type. This means defining a pair of function overloads using the two types as parameter types may violate the One Definition Rule. Since iterator is convertible to const_iterator, a single function with a const_iterator as parameter type will work instead.


Feature-test macro Value Std Feature
__cpp_lib_flat_map 202207L (C++23) std::flat_map and std::flat_multimap

[edit] Example

[edit] See also

(C++23)
adapts two containers to provide a collection of key-value pairs, sorted by unique keys
(class template) [edit]
collection of key-value pairs, sorted by keys
(class template) [edit]
collection of key-value pairs, hashed by keys
(class template) [edit]