Académique Documents
Professionnel Documents
Culture Documents
<utility>
std::pair
template <class T1, class T2> struct pair;
Pair of values
This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through its
public members first and second.
Pairs are a particular case of tuple.
Template parameters
T1
Type of member first, aliased as first_type.
T2
Type of member second, aliased as second_type.
Member types
member type
first_type
definition
The first template parameter (T1)
notes
Type of member first.
second_type
Member variables
member variable
definition
first
second
Member functions
(constructor)
Construct pair (public member function )
pair::operator=
Assign contents (public member function )
pair::swap
Swap contents (public member function )
Non-member function overloads
relational operators (pair)
Relational operators for pair (function template )
swap (pair)
Exchanges the contents of two pairs (function template )
get (pair)
Get element (tuple interface) (function template )
Non-member class specializations
tuple_element<pair>
Tuple element type for pair (class template specialization )
tuple_size<pair>
Tuple traits for pair (class template specialization )
See also
make_pair
Construct pair object (function template )
piecewise_construct
Piecewise construct constant (constant )
function template
<utility>
std::make_pair
C++98
C++11
C++98
C++11
foo: 10, 20
bar: 10, 65
If either (or both) T1 or T2 is an rvalue reference type of a type supporting move semantics, its corresponding
argument is modified.
Exception safety
The function provides the same level of guarantees to each object as provided by the corresponding element
constructor.
Construct pair (public member function )
function template
<utility>
std::get (pair)
lvalue (1)
rvalue (2)
const (3)
get (pair<T1,T2>&
pr) noexcept;
6
7
8
9
10
11
12
13
14
// accessing pairs with get
#include <utility>
// std::pair, std::get
#include <iostream>
// std::cout
int main () {
std::pair <int,char> foo (10,'x');
std::get<0>(foo) = 50;
std::cout << "foo contains: ";
std::cout << std::get<0>(foo) << " and " << std::get<1>(foo) << '\n';
return 0;
}
Edit & Run
Output:
foo contains: 50 and x
Data races
One of the members of pr is potentially accessed or modified by the caller. Concurrently accessing the other is
safe.
Exception safety
No-throw guarantee: this function never throws exceptions.
header
<list>
List header
Header that defines the list container class:
Classes
list
List (class template )
Functions
begin
Iterator to beginning (function template )
end
Iterator to end (function template )
function template
<iterator> <array> <deque> <forward_list> <list> <map> <regex> <set> <string> <unordered_map>
<unordered_set> <vector>
std::begin
C++11
C++14
container (1) template <class Container>
auto begin (Container& cont) -> decltype
(cont.begin());
template <class Container>
auto begin (const Container& cont) -> decltype
(cont.begin());
array (2) template <class T, size_t N>
T* begin (T(&arr)[N]);
Iterator to beginning
Returns an iterator pointing to the first element in the sequence:
(1) Container
The function returns cont.begin().
(2) Array
The function returns the array-to-pointer conversion of its argument.
If the sequence is empty, the returned value shall not be dereferenced.
These function templates are defined in multiple headers: Each of these headers includes the generic templates for all container and array
types and not simply a specific overload. The headers
are: <iterator>, <array>, <deque>,<forward_list>, <list>, map, <regex>, <set>, <string>, <unordered_map>, <
unordered_set> and <vector>.
Conversely, begin is overloaded (with a different definition) in headers <initializer_list> and <valarray>.
Parameters
cont
An object of a class type for which member begin is defined.
arr
An array.
Return Value
For (1), the same as returned by cont.begin().
For (2), a pointer to the first element in the array.
Example
1 // std::begin / std::end example
2 #include <iostream>
// std::cout
3 #include <vector>
// std::vector, std::begin, std::end
4
5 int main () {
6 int foo[] = {10,20,30,40,50};
7 std::vector<int> bar;
8
9 // iterate foo: inserting into bar
10 for (auto it = std::begin(foo); it!=std::end(foo); ++it)
11
bar.push_back(*it);
12
13 // iterate bar: print contents:
14 std::cout << "bar contains:";
15 for (auto it = std::begin(bar); it!=std::end(bar); ++it)
16
std::cout << ' ' << *it;
17 std::cout << '\n';
18
19 return 0;
20 }
Output:
bar contains: 10 20 30 40 50
Data races
The argument is accessed but not modified.
None of the elements in the sequence are accessed by the call, but the iterator returned can be used to access or modify them.
Exception safety
Provides the same level of guarantees as the operation performed on the argument (for standard containers and arrays this is a no-throw
guarantee).
function template
<iterator> <array> <deque> <forward_list> <list> <map> <regex> <set> <string> <unordered_map>
<unordered_set> <vector>
std::end
C++11
C++14
container (1)
array (2)
Iterator to end
Returns an iterator pointing to the past-the-end element in the sequence:
(1) Container
The function returns cont.end().
(2) Array
The function returns arr+N.
If the sequence is empty, the returned value compares equal to the one returned by begin with the same argument.
These function templates are defined in multiple headers: Each of these headers includes the generic templates for all container and array
types and not simply a specific overload. The headers
are: <iterator>, <array>, <deque>,<forward_list>, <list>, map, <regex>, <set>, <string>, <unordered_map>, <
unordered_set> and <vector>.
Conversely, end is overloaded (with a different definition) in headers <initializer_list> and <valarray>.
Parameters
cont
An object of a class type for which member end is defined.
arr
An array.
Return Value
For (1), the same as returned by cont.end().
For (2), a pointer to the element that would follow the last element in the array.
Example
1 // std::begin / std::end example
2 #include <iostream>
// std::cout
3 #include <vector>
// std::vector, std::begin, std::end
4
5 int main () {
6 int foo[] = {10,20,30,40,50};
7 std::vector<int> bar;
8
9 // iterate foo: inserting into bar
10 for (auto it = std::begin(foo); it!=std::end(foo); ++it)
11
bar.push_back(*it);
12
13 // iterate bar: print contents:
14 std::cout << "bar contains:";
15 for (auto it = std::begin(bar); it!=std::end(bar); ++it)
16
std::cout << ' ' << *it;
17 std::cout << '\n';
18
19 return 0;
20 }
Output:
bar contains: 10 20 30 40 50
Data races
The argument is accessed but not modified.
None of the elements in the sequence are accessed by the call, but the iterator returned can be used to access or modify them.
Exception safety
Provides the same level of guarantees as the operation performed on the argument (for standard containers and arrays this is a no-throw
guarantee).
class template
<queue>
std::queue
template <class T, class Container = deque<T> > class queue;
FIFO queue
queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into
one end of the container and extracted from the other.
queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as
its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the
specific container and popped from its "front".
The underlying container may be one of the standard container class template or some other specifically designed container class. This
underlying container shall support at least the following operations:
empty
size
front
back
push_back
pop_front
The standard container classes deque and list fulfill these requirements. By default, if no container class is specified for a
particular queue class instantiation, the standard container deque is used.
Template parameters
T
Type of the elements.
Aliased as member type queue::value_type.
Container
Type of the internal underlying container object where the elements are stored.
Its value_type shall be T.
Aliased as member type queue::container_type.
Member types
C++98
C++11
member type
value_type
definition
The first template parameter (T)
notes
Type of the elements
container_type
size_type
Member functions
(constructor)
Construct queue (public member function )
empty
Test whether container is empty (public member function )
size
Return size (public member function )
front
Access next element (public member function )
back
Access last element (public member function )
push
Insert element (public member function )
emplace
Construct and insert element (public member function )
pop
Remove next element (public member function )
swap
Swap contents (public member function )
empty
size
back
push_back
pop_back
The standard container classes vector, deque and list fulfill these requirements. By default, if no container class is specified for a
particular stack class instantiation, the standard container deque is used.
Template parameters
T
Type of the elements.
Aliased as member type stack::value_type.
Container
Type of the internal underlying container object where the elements are stored.
Its value_type shall be T.
Aliased as member type stack::container_type.
Member types
C++98
C++11
member type
value_type
definition
The first template parameter (T)
container_type
size_type
Member functions
(constructor)
Construct stack (public member function )
empty
Test whether container is empty (public member function )
size
Return size (public member function )
top
Access next element (public member function )
push
Insert element (public member function )
emplace
Construct and insert element (public member function )
pop
Remove top element (public member function )
swap
Swap contents (public member function )
notes
std::set
template < class T,
class Compare = less<T>,
class Alloc = allocator<T>
> class set;
Set
// set::key_type/value_type
// set::key_compare/value_compare
// set::allocator_type
Sets are containers 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 T), 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 binary search trees.
Container properties
Associative
Elements in associative containers are referenced by their key and not by their absolute position in the container.
Ordered
The elements in the container follow a strict order at all times. All inserted elements are given a position in this order.
Set
The value of an element is also the key used to identify it.
Unique keys
No two elements in the container can have equivalent keys.
Allocator-aware
The container uses an allocator object to dynamically handle its storage needs.
Template parameters
T
Type of the elements. Each element in a set container is also uniquely identified by this value (each value is itself also the
element's key).
Aliased as member types set::key_type and set::value_type.
Compare
A binary predicate that takes two arguments of the same type as the elements and returns a bool. The expressioncomp(a,b),
where comp is an object of this type and a and b are key values, shall return true if a is considered to go before b in the strict
weak ordering the function defines.
The set object uses this expression to determine both the order the elements follow in the container and whether two element keys
are equivalent (by comparing them reflexively: they are equivalent if !comp(a,b) && !comp(b,a)). No two elements in
a set container can be equivalent.
This can be a function pointer or a function object (see constructor for an example). This defaults to less<T>, which returns the
same as applying the less-than operator (a<b).
Aliased as member types set::key_compare and set::value_compare.
Alloc
Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which
defines the simplest memory allocation model and is value-independent.
Aliased as member type set::allocator_type.
Member types
C++98
C++11
member type
key_type
definition
The first template parameter (T)
notes
value_type
key_compare
value_compare
allocator_type
defaults
to:allocator<value_type>
reference
allocator_type::reference
for the
default allocator:value_type&
const_reference
allocator_type::const_reference
for the
default allocator: const
value_type&
pointer
allocator_type::pointer
for the
default allocator:value_type*
const_pointer
allocator_type::const_pointer
for the
default allocator: const
value_type*
iterator
convertible
to const_iterator
difference_type
size_type
an unsigned integral type that can represent any nonusually the same as size_t
negative value of difference_type
Member functions
(constructor)
Construct set (public member function )
(destructor)
Set destructor (public member function )
operator=
Copy container content (public member function )
Iterators:
begin
Return iterator to beginning (public member function )
end
Return iterator to end (public member function )
rbegin
Return reverse iterator to reverse beginning (public member function )
rend
Return reverse iterator to reverse end (public member function )
cbegin
Return const_iterator to beginning (public member function )
cend
Return const_iterator to end (public member function )
crbegin
Return const_reverse_iterator to reverse beginning (public member function )
crend
Return const_reverse_iterator to reverse end (public member function )
Capacity:
empty
class template
std::map
template < class Key,
class T,
class Compare = less<Key>,
class Alloc = allocator<pair<const Key,T> >
> class map;
Map
<map>
//
//
//
//
map::key_type
map::mapped_type
map::key_compare
map::allocator_type
Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.
In a map, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated
to this key. The types of key and mapped value may differ, and are grouped together in member typevalue_type, which is a pair type
combining both:
typedef pair<const Key, T> value_type;
Internally, the elements in a map are always sorted by its key following a specific strict weak ordering criterion indicated by its
internal comparison object (of type Compare).
map containers are generally slower than unordered_map containers to access individual elements by their key, but they allow the direct
iteration on subsets based on their order.
The mapped values in a map can be accessed directly by their corresponding key using the bracket operator ((operator[]).
Maps are typically implemented as binary search trees.
Container properties
Associative
Elements in associative containers are referenced by their key and not by their absolute position in the container.
Ordered
The elements in the container follow a strict order at all times. All inserted elements are given a position in this order.
Map
Each element associates a key to a mapped value: Keys are meant to identify the elements whose main content is the mapped value.
Unique keys
No two elements in the container can have equivalent keys.
Allocator-aware
The container uses an allocator object to dynamically handle its storage needs.
Template parameters
Key
Type of the keys. Each element in a map is uniquely identified by its key value.
Aliased as member type map::key_type.
T
Type of the mapped value. Each element in a map stores some data as its mapped value.
Aliased as member type map::mapped_type.
Compare
A binary predicate that takes two element keys as arguments and returns a bool. The expression comp(a,b), where comp is an
object of this type and a and b are key values, shall return true if a is considered to go before bin the strict weak ordering the
function defines.
The map object uses this expression to determine both the order the elements follow in the container and whether two element keys
are equivalent (by comparing them reflexively: they are equivalent if !comp(a,b) && !comp(b,a)). No two elements in
a map container can have equivalent keys.
This can be a function pointer or a function object (see constructor for an example). This defaults to less<T>, which returns the
same as applying the less-than operator (a<b).
Aliased as member type map::key_compare.
Alloc
Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which
defines the simplest memory allocation model and is value-independent.
Aliased as member type map::allocator_type.
Member types
C++98
C++11
member type
key_type
definition
The first template parameter (Key)
notes
mapped_type
value_type
key_compare
value_compare
allocator_type
see value_comp
defaults
to:allocator<value_type>
reference
allocator_type::reference
for the
default allocator:value_type&
const_reference
allocator_type::const_reference
for the
default allocator: const
value_type&
pointer
allocator_type::pointer
for the
default allocator:value_type*
const_pointer
allocator_type::const_pointer
for the
default allocator: const
value_type*
iterator
convertible
to const_iterator
difference_type
size_type
an unsigned integral type that can represent any nonusually the same as size_t
negative value of difference_type
Member functions
(constructor)
Construct map (public member function )
(destructor)
Map destructor (public member function )
operator=
Copy container content (public member function )
Iterators:
begin
Return iterator to beginning (public member function )
end
Return iterator to end (public member function )
rbegin
upper_bound
Return iterator to upper bound (public member function )
equal_range
Get range of equal elements (public member function )
Allocator:
get_allocator
Get allocator (public member function )
public member function
<map>
std::map::map
C++98
C++11
C++14
empty (1)
range (2)
copy (3)
Construct map
Constructs a map container object, initializing its contents depending on the constructor version used:
C++98
C++11
The code does not produce any output, but demonstrates some ways in which a map container can be constructed.
Complexity
Constant for the empty constructors (1), and for the move constructors (4) (unless alloc is different from x's allocator).
For all other cases, linear in the distance between the iterators (copy constructions) if the elements are already sorted according to the same
criterion. For unsorted sequences, linearithmic (N*logN) in that distance (sorting,copy constructions).
Iterator validity
The move constructors (4), invalidate all iterators, pointers and references related to x if the elements are moved.
Data races
All copied elements are accessed.
The move constructors (4) modify x.
Exception safety
Strong guarantee: no effects in case an exception is thrown.
If allocator_traits::construct is not supported with the appropriate arguments for the element constructions, or if the range specified
by [first,last) is not valid, it causes undefined behavior.
See also
map::operator=
Copy container content (public member function )
map::insert
Insert elements (public member function )