You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sets are containers (template classes) that store unique elements
following a specific order.
In a set, the value of an element also identifies it (the value is itself
the key, of type Key), and each value must be unique.
The value of the elements in a set cannot be modified once in the
container (the elements are always const), but they can be inserted or
removed from the container.
Internally, the elements in a set are always sorted following a
specific strict weak ordering criterion indicated by its internal
comparison object (of type Compare).
Set containers are generally slower than unordered_set containers to
access individual elements by their key, but they allow the direct
iteration on subsets based on their order.
Sets are typically implemented as red–black trees.
// Default constructor. Constructs an empty container.
std::set< Type, Compare > set_name;
// Copy-constructs the temporary `Compare` class object. Constructs an empty container.
std::set< Type, Compare > rset_name( Compare( true ) );
// Copy-constructs the comparison functor `comp` with the contents of compare. Constructs an empty container.
Compare comp;
std::set< Type, Compare > set_name( comp );
// Default constructor. Constructs an empty container.
std::set< Type > set_name1;
// Copy constructor.
std::set< Type > set_name2( set_name1 );
// Default constructor. Constructs an empty container.
std::set< Type > set_name1;
// Copy constructor.
std::set< Type > set_name2 = set_name1;
// Default constructor. Constructs an empty container.
std::set< Type > set_name1;
// Move constructor.
std::set< Type > set_name2( std::move( set_name1 ) );
// Initializer list constructor.
std::set< Type > set_name1{ ... };
// Constructs the container with the contents of the range `[first, last)`.
Compare comp;
std::set< Type > set_name2( set_name1.begin(), set_name1.end(), comp /* optional */ );
std::set< Type >* set_ptr = new std::set< Type >;
// Default constructor. Constructs an empty container.
std::set< Type > set_name;
std::set< Type >* set_ptr = new std::set< Type >( set_name );
Compare: A Compare type providing a strict weak ordering. By default, the
first key (smallest key) is at the beginning of the set because its default
Compare is std::less< Key >, which sorts the elements in ascending order.
Allocator: An allocator that is used to acquire/release memory and to
construct/destroy the elements in that memory.
Member Types
key_type: Key.
value_type: Key.
size_type: Unsigned integer type (usually std::size_t).
difference_type: Signed integer type (usually std::ptrdiff_t).
instantiated with template arguments iterator and node_type.
Member Functions
(constructor): Constructs the set (public member function).
(destructor): Destructs the set (public member function).
operator=: Assigns values to the container (public member function).
get_allocator: Returns the associated allocator (public member function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
rbegin, crbegin: Returns a reverse iterator to the beginning (public
member function).
rend, crend: Returns a reverse iterator to the end (public member
function).
empty: Checks whether the container is empty (public member function).
size: Returns the number of elements (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
clear: Clears the contents (public member function).
insert: Inserts elements or nodes(since C++17) (public member function).
insert_range (C++23): Inserts a range of elements (public member
function).
emplace: Constructs elements in-place (public member function).
emplace_hint: Constructs elements in-place using a hint (public member
function).
erase: Erases elements and returns a valid iterator (public member
function).
swap: Swaps the contents (public member function).
extract (C++17): Extracts nodes from the container (public member
function).
merge (C++17): Splices nodes from another container (public member
function).
count: Returns the number of elements matching specific key (public member
function).
find: Finds an element with specific key (public member function).
contains (C++20): Checks if the container contains an element with
specific key (public member function).
equal_range: Returns range of elements matching a specific key (public
member function).
lower_bound: Returns an iterator to the first element not less than the
given key (public member function).
upper_bound: Returns an iterator to the first element greater than the
given key (public member function).
key_comp: Returns the function that compares keys (public member
function).
value_comp: Returns the function that compares keys in objects of type
value_type (public member function).
Non-member Functions
operator==, operator!=/</<=/>/>= (removed in C++20), operator<=>
(C++20): Lexicographically compares the values of two sets (function
template).
std::swap( std::set ): Specializes the std::swap algorithm (function
template).
erase_if( std::set ) (C++20): Erases all elements satisfying specific
criteria (function template).
Multisets
Explanation
Multisets are containers (template classes) that store elements
following a specific order, and where multiple elements can have
equivalent values.
In a multiset, the value of an element also identifies it (the value is
itself the key, of type Key).
The value of the elements in a multiset cannot be modified once in the
container (the elements are always const), but they can be inserted or
removed from the container.
Internally, the elements in a multiset are always sorted following a
specific strict weak ordering criterion indicated by its internal
comparison object (of type Compare).
Multiset containers are generally slower thanunordered_multiset
containers to access individual elements by their key, but they allow
the direct iteration on subsets based on their order.
Multisets are typically implemented as red–black trees.
// Default constructor. Constructs an empty container.
std::multiset< Type, Compare > mset_name;
// Copy-constructs the temporary `Compare` class object. Constructs an empty container.
std::multiset< Type, Compare > rmset_name( Compare( true ) );
// Copy-constructs the comparison functor `comp` with the contents of compare. Constructs an empty container.
Compare comp;
std::multiset< Type, Compare > mset_name( comp );
// Default constructor. Constructs an empty container.
std::multiset< Type > mset_name1;
// Copy constructor.
std::multiset< Type > mset_name2( mset_name1 );
// Default constructor. Constructs an empty container.
std::multiset< Type > mset_name1;
// Copy constructor.
std::multiset< Type > mset_name2 = mset_name1;
// Default constructor. Constructs an empty container.
std::multiset< Type > mset_name1;
// Move constructor.
std::multiset< Type > mset_name2( std::move( mset_name1 ) );
// Initializer list constructor.
std::set< Type, Compare > mset_name1{ ... };
// Constructs the container with the contents of the range `[first, last)`.
Compare comp;
std::set< Type, Compare > mset_name2( mset_name1.begin(), mset_name1.end(), comp /* optional */ );
std::multiset< Type >* mset_ptr = new std::multiset< Type >;
// Default constructor. Constructs an empty container.
std::multiset< Type > mset_name;
std::multiset< Type >* mset_ptr = new std::multiset< Type >( mset_name );
Compare: A Compare type providing a strict weak ordering. By default, the
first key (smallest key) is at the beginning of the multiset because its
default Compare is std::less< Key >, which sorts the elements in
ascending order.
Allocator: An allocator that is used to acquire/release memory and to
construct/destroy the elements in that memory.
Member Types
key_type: Key.
value_type: Key.
size_type: Unsigned integer type (usually std::size_t).
difference_type: Signed integer type (usually std::ptrdiff_t).
node_type (since C++17): A specialization of node handle representing a
container node.
Member Functions
(constructor): Constructs the multiset (public member function).
(destructor): Destructs the multiset (public member function).
operator=: Assigns values to the container (public member function).
get_allocator: Returns the associated allocator (public member function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
rbegin, crbegin: Returns a reverse iterator to the beginning (public
member function).
rend, crend: Returns a reverse iterator to the end (public member
function).
empty: Checks whether the container is empty (public member function).
size: Returns the number of elements (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
clear: Clears the contents (public member function).
insert: Inserts elements or nodes(since C++17) (public member function).
insert_range (C++23): Inserts a range of elements (public member
function).
emplace: Constructs elements in-place (public member function).
emplace_hint: Constructs elements in-place using a hint (public member
function).
erase: Erases elements and returns a valid iterator (public member
function).
swap: Swaps the contents (public member function).
extract (C++17): Extracts nodes from the container (public member
function).
merge (C++17): Splices nodes from another container (public member
function).
count: Returns the number of elements matching specific key (public member
function).
find: Finds an element with specific key (public member function).
contains (C++20): Checks if the container contains an element with
specific key (public member function).
equal_range: Returns range of elements matching a specific key (public
member function).
lower_bound: Returns an iterator to the first element not less than the
given key (public member function).
upper_bound: Returns an iterator to the first element greater than the
given key (public member function).
key_comp: Returns the function that compares keys (public member
function).
value_comp: Returns the function that compares keys in objects of type
value_type (public member function).
Non-member Functions
operator==, operator!=/</<=/>/>= (removed in C++20), operator<=>
(C++20): Lexicographically compares the values of two multisets (function
template).
std::swap( std::multiset ): Specializes the std::swap algorithm (function
template).
erase_if( std::multiset ) (C++20): Erases all elements satisfying specific
criteria (function template).
Unordered Sets
Explanation
Unordered sets are containers (template classes) that store unique
elements in no particular order, and which allow for fast retrieval
of individual elements based on their value.
In an unordered_set, the value of an element is at the same time its
key, that identifies it uniquely.
Keys are immutable, therefore, the elements in an unordered_setcannot be modified once in the container - they can be inserted and
removed, though.
Internally, the elements in the unordered_set are not sorted in any
particular order, but organized into buckets depending on their hash
values to allow for fast access to individual elements directly by
their values (with a constant average time complexity on average).
unordered_set containers are faster than set containers to access
individual elements by their key, although they are generally less
efficient for range iteration through a subset of their elements.
Iterators in the container are at least forward iterators.
Their header files is <unordered_set>.
Declaration Syntax
std::unordered_set< Type > uset_name;
// Not common, not recommendstructHash {
std::size_toperator()( const Type& obj ) const {
// This is only an example.return std::hash< SubType1 >()( obj._mem1 )
^ std::hash< SubType2 >()( obj._mem2 );
};
};
structKeyEqual {
booloperator()( const Type& lhs, const Type& rhs ) const {
// This is only an example.return lhs.id == rhs.id; // Custom equality based on id only
};
};
std::unordered_set< Type, Hash, KeyEqual > uset_name;
std::unordered_set< Type >* uset_ptr;
Initialization Syntax
// Initializer list constructor.
std::unordered_set< Type > uset_name = { ... };
// Initializer list constructor.
std::unordered_set< Type > uset_name{ ... };
// Default constructor. Constructs an empty container.
std::unordered_set< Type > uset_name;
// Default constructor. Constructs an empty container.
std::unordered_set< Type > uset_name1;
// Copy constructor.
std::unordered_set< Type > uset_name2( uset_name1 );
// Default constructor. Constructs an empty container.
std::unordered_set< Type > uset_name1;
// Copy constructor.
std::unordered_set< Type > uset_name2 = uset_name1;
// Default constructor. Constructs an empty container.
std::unordered_set< Type > uset_name1;
// Move constructor.
std::unordered_set< Type > uset_name2( std::move( uset_name1 ) );
// Initializer list constructor.
std::set< Type, Hash, KeyEqual > uset_name1{ ... };
// Constructs the container with the contents of the range `[first, last)`.
Hash hash;
KeyEqual key_equal;
std::set< Type, Hash, KeyEqual > uset_name2( uset_name1.begin(), uset_name1.end(), size /* optional */, hash /* optional */, key_equal /* optional */ );
std::unordered_set< Type >* uset_ptr = new std::unordered_set< Type >;
// Default constructor. Constructs an empty container.
std::unordered_set< Type > uset_name;
std::unordered_set< Type >* uset_ptr = new std::unordered_set< Type >( uset_name );
Hash: A unary function object type that takes an object of the same type as
the elements as argument and returns a unique value of type size_t based on
it. This can either be a class implementing a function call operator or a
pointer to a function (see constructor for an example). This defaults to
std::hash< Key >.
KeyEqual: A binary predicate that takes two arguments of the same type as
the elements and returns a bool. The expression KeyEqual( a,b ), where
KeyEqual is an object of this type and a and b are key values, shall
return true if a is to be considered equivalent to b.
Allocator: An allocator that is used to acquire/release memory and to
construct/destroy the elements in that memory.
Member Types
key_type: Key.
value_type: Key.
size_type: Unsigned integer type (usually std::size_t).
difference_type: Signed integer type (usually std::ptrdiff_t).
iterator: Constant LegacyForwardIterator to value_type.
const_iterator: LegacyForwardIterator to const value_type.
local_iterator: An iterator type whose category, value, difference,
pointer and reference types are the same as iterator. This iterator can be
used to iterate through a single bucket but not across buckets
const_local_iterator: An iterator type whose category, value, difference,
pointer and reference types are the same as const_iterator. This iterator
can be used to iterate through a single bucket but not across buckets
node_type (since C++17): A specialization of node handle representing a
container node.
insert_return_type (since C++17): Type describing the result of inserting
a node_type, a specialization of
instantiated with template arguments iterator and node_type.
Member Functions
(constructor): Constructs the unordered set (public member function).
(destructor): Destructs the unordered set (public member function).
operator=: Assigns values to the container (public member function).
get_allocator: Returns the associated allocator (public member function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
empty: Checks whether the container is empty (public member function).
size: Returns the number of elements (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
clear: Clears the contents (public member function).
insert: Inserts elements or nodes(since C++17) (public member function).
insert_range (C++23): Inserts a range of elements (public member
function).
emplace: Constructs elements in-place (public member function).
emplace_hint: Constructs elements in-place using a hint (public member
function).
erase: Erases elements and returns a valid iterator (public member
function).
swap: Swaps the contents (public member function).
extract (C++17): Extracts nodes from the container (public member
function).
merge (C++17): Splices nodes from another container (public member
function).
count: Returns the number of elements matching specific key (public member
function).
find: Finds an element with specific key (public member function).
contains (C++20): Checks if the container contains an element with
specific key (public member function).
equal_range: Returns range of elements matching a specific key (public
member function).
begin( size_type ), cbegin( size_type ): Returns an iterator to the
beginning of the specified bucket (public member function).
end( size_type ), cend( size_type ): Returns an iterator to the end of
the specified bucket (public member function).
bucket_count: Returns the number of buckets (public member function).
max_bucket_count: Returns the maximum number of buckets (public member
function).
bucket_size: Returns the number of elements in specific bucket (public
member function).
bucket: Returns the bucket for specific key (public member function).
load_factor: Returns average number of elements per bucket (public member
function).
max_load_factor: Manages maximum average number of elements per bucket
(public member function).
rehash: Reserves at least the specified number of buckets and regenerates
the hash table (public member function).
reserve: Reserves space for at least the specified number of elements and
regenerates the hash table (public member function).
hash_function: Returns function used to hash the keys (public member
function).
key_eq: Returns the function used to compare keys for equality (public
member function).
Non-member Functions
operator==, operator!= (removed in C++20): Lexicographically compares the
values of two unordered sets (function template).
std::swap( std::unordered_set ): Specializes the std::swap algorithm
(function template).
erase_if( std::unordered_set ) (C++20): Erases all elements satisfying
specific criteria (function template).
Unordered Multisets
Explanation
Unordered multisets are containers (template classes) that store
elements in no particular order, allowing fast retrieval of
individual elements based on their value, much like unordered_set
containers, but allowing different elements to have equivalent
values.
In an unordered_multiset, the value of an element is at the same time its
key, used to identify it.
Keys are immutable, therefore, the elements in an unordered_multisetcannot be modified once in the container - they can be inserted and
removed, though.
Internally, the elements in the unordered_multiset are not sorted in any
particular, but organized into buckets depending on their hash
values to allow for fast access to individual elements directly by
their values (with a constant average time complexity on average).
Elements with equivalent values are grouped together in the same
bucket and in such a way that an iterator (see equal_range) can iterate
through all of them.
Iterators in the container are at least forward iterators.
Their header files is <unordered_set>.
Declaration Syntax
std::unordered_multiset< Type > umset_name;
// Not common, not recommendstructHash {
std::size_toperator()( const Type& obj ) const {
// This is only an example.return std::hash< SubType1 >()( obj._mem1 )
^ std::hash< SubType2 >()( obj._mem2 );
};
};
structKeyEqual {
booloperator()( const Type& lhs, const Type& rhs ) const {
// This is only an example.return lhs.id == rhs.id; // Custom equality based on id only
};
};
std::unordered_multiset< Type, Hash, KeyEqual > umset_name;
std::unordered_multiset< Type >* umset_ptr;
Initialization Syntax
// Initializer list constructor.
std::unordered_multiset< Type > umset_name = { ... };
// Initializer list constructor.
std::unordered_multiset< Type > umset_name{ ... };
// Default constructor. Constructs an empty container.
std::unordered_multiset< Type > umset_name;
// Default constructor. Constructs an empty container.
std::unordered_multiset< Type > umset_name1;
// Copy constructor.
std::unordered_multiset< Type > umset_name2( umset_name1 );
// Default constructor. Constructs an empty container.
std::unordered_multiset< Type > umset_name1;
// Copy constructor.
std::unordered_multiset< Type > umset_name2 = umset_name1;
// Default constructor. Constructs an empty container.
std::unordered_multiset< Type > umset_name1;
// Move constructor.
std::unordered_multiset< Type > umset_name2( std::move( umset_name1 ) );
// Initializer list constructor.
std::set< Type, Hash, KeyEqual > umset_name1{ ... };
// Constructs the container with the contents of the range `[first, last)`.
Hash hash;
KeyEqual key_equal;
std::set< Type, Hash, KeyEqual > umset_name2( umset_name1.begin(), umset_name1.end(), size /* optional */, hash /* optional */, key_equal /* optional */ );
std::unordered_multiset< Type >* umset_ptr = new std::unordered_multiset< Type >;
// Default constructor. Constructs an empty container.
std::unordered_multiset< Type > umset_name;
std::unordered_multiset< Type >* umset_ptr = new std::unordered_multiset< Type >( umset_name );
Syntax for Deleting std::unordered_multiset Pointers
Hash: A unary function object type that takes an object of the same type as
the elements as argument and returns a unique value of type size_t based on
it. This can either be a class implementing a function call operator or a
pointer to a function (see constructor for an example). This defaults to
std::hash< Key >.
KeyEqual: A binary predicate that takes two arguments of the same type as
the elements and returns a bool. The expression KeyEqual( a,b ), where
KeyEqual is an object of this type and a and b are key values, shall
return true if a is to be considered equivalent to b.
Allocator: An allocator that is used to acquire/release memory and to
construct/destroy the elements in that memory.
Member Types
key_type: Key.
value_type: Key.
size_type: Unsigned integer type (usually std::size_t).
difference_type: Signed integer type (usually std::ptrdiff_t).
iterator: Constant LegacyForwardIterator to value_type.
const_iterator: LegacyForwardIterator to const value_type.
local_iterator: An iterator type whose category, value, difference,
pointer and reference types are the same as iterator. This iterator can be
used to iterate through a single bucket but not across buckets
const_local_iterator: An iterator type whose category, value, difference,
pointer and reference types are the same as const_iterator. This iterator
can be used to iterate through a single bucket but not across buckets
node_type (since C++17): A specialization of node handle representing a
container node.
Member Functions
(constructor): Constructs the unordered multiset (public member function).
(destructor): Destructs the unordered multiset (public member function).
operator=: Assigns values to the container (public member function).
get_allocator: Returns the associated allocator (public member function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
empty: Checks whether the container is empty (public member function).
size: Returns the number of elements (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
clear: Clears the contents (public member function).
insert: Inserts elements or nodes(since C++17) (public member function).
insert_range (C++23): Inserts a range of elements (public member
function).
emplace: Constructs elements in-place (public member function).
emplace_hint: Constructs elements in-place using a hint (public member
function).
erase: Erases elements and returns a valid iterator (public member
function).
swap: Swaps the contents (public member function).
extract (C++17): Extracts nodes from the container (public member
function).
merge (C++17): Splices nodes from another container (public member
function).
count: Returns the number of elements matching specific key (public member
function).
find: Finds an element with specific key (public member function).
contains (C++20): Checks if the container contains an element with
specific key (public member function).
equal_range: Returns range of elements matching a specific key (public
member function).
begin( size_type ), cbegin( size_type ): Returns an iterator to the
beginning of the specified bucket (public member function).
end( size_type ), cend( size_type ): Returns an iterator to the end of
the specified bucket (public member function).
bucket_count: Returns the number of buckets (public member function).
max_bucket_count: Returns the maximum number of buckets (public member
function).
bucket_size: Returns the number of elements in specific bucket (public
member function).
bucket: Returns the bucket for specific key (public member function).
load_factor: Returns average number of elements per bucket (public member
function).
max_load_factor: Manages maximum average number of elements per bucket
(public member function).
rehash: Reserves at least the specified number of buckets and regenerates
the hash table (public member function).
reserve: Reserves space for at least the specified number of elements and
regenerates the hash table (public member function).
hash_function: Returns function used to hash the keys (public member
function).
key_eq: Returns the function used to compare keys for equality (public
member function).
Non-member Functions
operator==, operator!= (removed in C++20): Lexicographically compares the
values of two unordered multisets (function template).
std::swap( std::unordered_multiset ): Specializes the std::swap algorithm
(function template).
erase_if( std::unordered_multiset ) (C++20): Erases all elements satisfying
specific criteria (function template).
Flat Sets
Explanation
Flat sets are a container adaptor that store unique elements following
a specific order.
In a flat set, the value of an element also identifies it (the value is
itself the key, of type Key), and each value must be unique.
The value of the elements in a flat set cannot be modified once in
the container (the elements are always const), but they can be inserted or
removed from the container.
Internally, the elements in a flat_set are stored in a sorted
manner using a contiguous memory structure, which allows for
efficient access and iteration.
flat_set containers are generally faster than set containers for
accessing individual elements by their key due to their underlying
array structure, but they require that elements be kept sorted upon
insertion.
flat_set is typically implemented as a sorted vector, allowing for
efficient searching and iteration.
The class templateflat_setacts as a wrapper to the underlying
sorted container passed as object of type KeyContainer.
The header file for flat_set is <experimental/flat_set> (or may be found
in other namespaces in different implementations).
// Not common, not recommend
std::flat_set< Type, Compare, KeyContainer< Type > > fset_name;
std::flat_set< Type >* fset_ptr;
Initialization Syntax
// Initializer list constructor.
std::flat_set< Type > fset_name = { ... };
// Initializer list constructor.
std::flat_set< Type > fset_name{ ... };
// Default constructor. Constructs an empty container adaptor.
std::flat_set< Type > fset_name;
// Default constructor. Constructs an empty container adaptor.
std::flat_set< Type, Compare, KeyContainer< Type > > fset_name;
// Constructs the underlying container by copying the contents of the container `cont`.// Construct a default `comp` to sort all elements.
KeyContainer< Type > cont = { ... };
std::flat_set< Type, Compare, KeyContainer< Type > > fset_name( cont );
// Constructs the underlying container by copying the contents of the container `cont`.// Copy the `comp` to sort all elements.
KeyContainer< Type > cont = { ... };
Compare comp;
std::flat_set< Type, Compare, KeyContainer< Type > > fset_name( cont, comp );
// Specify that all elements are unique. Just a tag.// Constructs the underlying container by `std::move( cont )`.// Construct a default `comp` to sort all elements.
std::sorted_unique_t s;
KeyContainer< Type > cont = { ... };
std::flat_set< Type, Compare, KeyContainer< Type > > fset_name( s, cont );
// Specify that all elements are unique. Just a tag.// Constructs the underlying container by `std::move( cont )`.// Copy the `comp` to sort all elements.
std::sorted_unique_t s:
KeyContainer< Type > cont = { ... };
Compare comp;
std::flat_set< Type, Compare, KeyContainer< Type > > fset_name( s, cont, comp );
// Specify that all elements are unique. Just a tag.// Move the content of the underlying container.// Copy the content of the `comp`.
std::sorted_unique_t s;
Compare comp;
std::flat_set< Type, Compare, KeyContainer< Type > > fset_name( s, KeyContainer< Type >{ ... }, comp );
// Copy-constructs the comparison functor `comp` with the contents of compare. Value-initializes the underlying container.
Compare comp;
std::flat_set< Type, Compare, KeyContainer< Type > > fset_name( comp );
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_set< Type > fset_name1;
// Copy constructor.
std::flat_set< Type > fset_name2( fset_name1 );
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_set< Type > fset_name1;
// Copy constructor.
std::flat_set< Type > fset_name2 = fset_name1;
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_set< Type > fset_name1;
// Move constructor.
std::flat_set< Type > fset_name2( std::move( fset_name1 ) );
// Specify that all elements are unique. Just a tag.// Constructs the underlying container by `std::move( cont )`.// Copy the `comp` to sort all elements.
std::sorted_unique_t s:
KeyContainer< Type > cont = { ... };
Compare comp;
std::flat_set< Type, Compare, KeyContainer< Type > > fset_name1( s, cont, comp );
// Constructs the container with the contents of the range `[first, last)`.
std::flat_set< Type, Compare, KeyContainer< Type > > fset_name2( s /* optional */, fset_name1.begin(), fset_name1.end(), comp /* optional */ );
std::flat_set< Type >* fset_ptr = new std::flat_set< Type >;
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_set< Type > fset_name;
std::flat_set< Type >* fset_ptr = new std::flat_set< Type >( fset_name );
Compare: A Compare type providing a strict weak ordering. By default, the
first key (smallest key) is at the beginning of the flat set because its
default Compare is std::less< Key >, which sorts the elements in
ascending order.
KeyContainer: The type of the underlying SequenceContainer to store the
elements. The iterators of such container should satisfy
LegacyRandomAccessIterator or model random_access_iterator. The standard
containers std::vector and std::deque satisfy these requirements.
c (private): The underlying container of container_type
(exposition-only member object*).
compare (private): The comparison function object of type key_compare
(exposition-only member object*).
Member Functions
(constructor): Constructs the flat set (public member function).
(destructor) (implicitly declared): Destroys every element of the container
adaptor (public member function).
operator=: Assigns values to the container adaptor (public member
function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
rbegin, crbegin: Returns a reverse iterator to the beginning (public
member function).
rend, crend: Returns a reverse iterator to the end (public member
function).
empty: Checks whether the container adaptor is empty (public member
function).
size: Returns the number of elements (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
emplace: Constructs elements in-place (public member function).
emplace_hint: Constructs elements in-place using a hint (public member
function).
insert: Inserts elements (public member function).
insert_range: Inserts a range of elements (public member function).
extract: Extracts the underlying container (public member function).
replace: Replaces the underlying container (public member function).
erase: Erases elements and returns a valid iterator (public member
function).
swap: Swaps the contents (public member function).
clear: Clears the contents (public member function).
find: Finds an element with specific key (public member function).
count: Returns the number of elements matching specific key (public member
function).
contains: Checks if the container contains an element with specific key
(public member function).
lower_bound: Returns an iterator to the first element not less than the
given key (public member function).
upper_bound: Returns an iterator to the first element greater than the
given key (public member function).
equal_range: Returns range of elements matching a specific key (public
member function).
key_comp: Returns the function that compares keys (public member
function).
value_comp: Returns the function that compares keys in objects of type
value_type (public member function).
Non-member Functions
operator==, operator<=>: Lexicographically compares the values of two
flat sets (function template).
std::swap( std::flat_set ): Specializes the std::swap algorithm (function
template).
erase_if( std::flat_set ): Erases all elements satisfying specific criteria
(function template).
Helper classes
std::uses_allocator< std::flat_set > (C++23): Specializes the
std::uses_allocat or type trait (class template specialization).
Tags
sorted_unique, sorted_unique_t (C++23): indicates that elements of a
range are sorted and unique (tag).
Flat Multisets
Explanation
Multilat sets are a container adaptor that store elements following a
specific order.
In a flat multiset, the value of an element also identifies it (the value
is itself the key, of type Key), but each value does not need to be
unique.
The value of the elements in a flat multiset cannot be modified once
in the container (the elements are always const), but they can be inserted
or removed from the container.
Internally, the elements in a flat_multiset are stored in a sorted
manner using a contiguous memory structure, which allows for
efficient access and iteration.
flat_multiset containers are generally faster than set containers for
accessing individual elements by their key due to their underlying
array structure, but they require that elements be kept sorted upon
insertion.
flat_multiset is typically implemented as a sorted vector, allowing for
efficient searching and iteration.
The class templateflat_multisetacts as a wrapper to the
underlying sorted container passed as object of type KeyContainer.
The header file for flat_multiset is <experimental/flat_set> (or may be
found in other namespaces in different implementations).
// Not common, not recommend
std::flat_multiset< Type, Compare, KeyContainer< Type > > fmset_name;
std::flat_multiset< Type >* fmset_ptr;
Initialization Syntax
// Initializer list constructor.
std::flat_multiset< Type > fmset_name = { ... };
// Initializer list constructor.
std::flat_multiset< Type > fmset_name{ ... };
// Default constructor. Constructs an empty container adaptor.
std::flat_multiset< Type > fmset_name;
// Default constructor. Constructs an empty container adaptor.
std::flat_multiset< Type, Compare, KeyContainer< Type > > fmset_name;
// Constructs the underlying container by copying the contents of the container `cont`.// Construct a default `comp` to sort all elements.
KeyContainer< Type > cont = { ... };
std::flat_multiset< Type, Compare, KeyContainer< Type > > fmset_name( cont );
// Constructs the underlying container by copying the contents of the container `cont`.// Copy the `comp` to sort all elements.
KeyContainer< Type > cont = { ... };
Compare comp;
std::flat_multiset< Type, Compare, KeyContainer< Type > > fmset_name( cont, comp );
// Allow different elements with the same value. Just a tag.// Constructs the underlying container by `std::move( cont )`.// Construct a default `comp` to sort all elements.
std::sorted_equivalent_t s;
KeyContainer< Type > cont = { ... };
std::flat_multiset< Type, Compare, KeyContainer< Type > > fmset_name( s, cont );
// Allow different elements with the same value. Just a tag.// Constructs the underlying container by `std::move( cont )`.// Copy the `comp` to sort all elements.
std::sorted_equivalent_t s;
KeyContainer< Type > cont = { ... };
Compare comp;
std::flat_multiset< Type, Compare, KeyContainer< Type > > fmset_name( s, cont, comp );
// Allow different elements with the same value. Just a tag.// Move the content of the underlying container.// Copy the content of the `comp`.
std::sorted_equivalent_t s;
Compare comp;
std::flat_multiset< Type, Compare, KeyContainer< Type > > fmset_name( s, KeyContainer< Type >{ ... }, comp );
// Copy-constructs the comparison functor `comp` with the contents of compare. Value-initializes the underlying container.
Compare comp;
std::flat_multiset< Type, Compare, KeyContainer< Type > > fmset_name( comp );
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_multiset< Type > fmset_name1;
// Copy constructor.
std::flat_multiset< Type > fmset_name2( fmset_name1 );
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_multiset< Type > fmset_name1;
// Copy constructor.
std::flat_multiset< Type > fmset_name2 = fmset_name1;
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_multiset< Type > fmset_name1;
// Move constructor.
std::flat_multiset< Type > fmset_name2( std::move( fmset_name1 ) );
// Allow different elements with the same value. Just a tag.// Constructs the underlying container by `std::move( cont )`.// Copy the `comp` to sort all elements.
std::sorted_equivalent_t s;
KeyContainer< Type > cont = { ... };
Compare comp;
std::flat_set< Type, Compare, KeyContainer< Type > > fmset_name1( s, cont, comp );
// Constructs the container with the contents of the range `[first, last)`.
std::flat_set< Type, Compare, KeyContainer< Type > > fmset_name2( s /* optional */, fmset_name1.begin(), fmset_name1.end(), comp /* optional */ );
std::flat_multiset< Type >* fmset_ptr = new std::flat_multiset< Type >;
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_multiset< Type > fmset_name;
std::flat_multiset< Type >* fmset_ptr = new std::flat_multiset< Type >( fmset_name );
Compare: A Compare type providing a strict weak ordering. By default, the
first key (smallest key) is at the beginning of the flat multiset because its
default Compare is std::less< Key >, which sorts the elements in
ascending order.
KeyContainer: The type of the underlying SequenceContainer to store the
elements. The iterators of such container should satisfy
LegacyRandomAccessIterator or model random_access_iterator. The standard
containers std::vector and std::deque satisfy these requirements.