CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 23 Jul 2025 08:45:27 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/utility/pair/pair/
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 23 Jul 2025 08:45:27 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"80e9-0SUx7CMqwwmrE7v1c3sOSuXEAbc"
Content-Encoding: gzip
Constructs a pair object.
This involves individually constructing its two component objects, with an initialization that depends on the constructor form invoked:
Most forms have two signatures: one taking const lvalue references, which copies the values into the pair, and one taking rvalue references, which moves them instead if their types support move semantics (for such types, the contents are transferred to the pair object and lost by their previous referrers, which are left in an unspecified but valid state).
Output:
The constructors taking rvalue references as arguments modify these arguments if their types support move semantics for this construction.
Otherwise, if any of the forms taking an rvalue reference as argument is called, and at least one of the types in the pair can be constructed with move semantics, the operation may leave pr in an invalid state in case of exception (no guarantees).
Otherwise, the function only implies copies and the operation produces no side effects (strong guarantee).
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
<utility>
classes
functions
types
-
piecewise_construct_tC++11
-
constants
-
piecewise_constructC++11
-
namespaces
pair
- pair::pair
member functions
- pair::operator=
-
pair::swapC++11
non-member overloads
-
get (pair)C++11
- relational operators (pair)
-
swap (pair)C++11
-
non-member specializations
-
tuple_element<pair>C++11
-
tuple_size<pair>C++11
-
public member function
<utility>
std::pair::pair
default (1) | pair(); |
---|---|
copy (2) | template<class U, class V> pair (const pair<U,V>& pr); |
initialization (3) | pair (const first_type& a, const second_type& b); |
default (1) | constexpr pair(); |
---|---|
copy / move (2) | template<class U, class V> pair (const pair<U,V>& pr);template<class U, class V> pair (pair<U,V>&& pr);pair (const pair& pr) = default;pair (pair&& pr) = default; |
initialization (3) | pair (const first_type& a, const second_type& b);template<class U, class V> pair (U&& a, V&& b); |
piecewise (4) | template <class... Args1, class... Args2> pair (piecewise_construct_t pwc, tuple<Args1...> first_args, tuple<Args2...> second_args); |
Construct pair
This involves individually constructing its two component objects, with an initialization that depends on the constructor form invoked:
- (1) default constructor
- Constructs a pair object with its elements value-initialized.
- (2) copy / move constructor (and implicit conversion)
- The object is initialized with the contents of the pr pair object.
The corresponding member of pr is passed to the constructor of each of its members. - (3) initialization constructor
- Member first is constructed with a and member second with b.
- (4) piecewise constructor
- Constructs members first and second in place, passing the elements of first_args as arguments to the constructor of first, and the elements of second_args to the constructor of second.
Most forms have two signatures: one taking const lvalue references, which copies the values into the pair, and one taking rvalue references, which moves them instead if their types support move semantics (for such types, the contents are transferred to the pair object and lost by their previous referrers, which are left in an unspecified but valid state).
Parameters
- pr
- Another pair object.
This may be an object of the same type as the object being constructed or of a pair type whose elements' types are implicitly convertible to those in the pair being constructed.
- a
- An object of the type of first, or some other type implicitly convertible to it.
- b
- An object of the type of second, or some other type implicitly convertible to it.
- pwc
- The piecewise_construct object.
The only purpose of this argument is to select the proper constructor signature. It conveys no information to be incorporated into the new object. - first_args, second_args
- tuple objects with the arguments to be passed to the constructors of members first and second.
Example
|
|
Output:
The price of lightbulbs is $0.99 The price of shoes is $39.9 The price of tomatoes is $2.3 |
Data races
The elements of pr, first_args and second_args are accessed.The constructors taking rvalue references as arguments modify these arguments if their types support move semantics for this construction.
Exception safety
If none of the individual constructions of members of pair can throw, the operation never throws exceptions (no-throw guarantee).Otherwise, if any of the forms taking an rvalue reference as argument is called, and at least one of the types in the pair can be constructed with move semantics, the operation may leave pr in an invalid state in case of exception (no guarantees).
Otherwise, the function only implies copies and the operation produces no side effects (strong guarantee).
See also
- pair::operator=
- Assign contents (public member function)
- make_pair
- Construct pair object (function template)
- tuple
- Tuple (class template)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us