Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/pair"

From cppreference.com
< cpp‎ | utility
(consider LWG2796 applied to C++98)
m (Helper specializations: - inline)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{cpp/title|pair}}
 
{{cpp/title|pair}}
 
{{cpp/utility/pair/navbar}}
 
{{cpp/utility/pair/navbar}}
{{ddcl | header=utility |
+
{{ddcl|header=utility|
 
template<
 
template<
 
     class T1,
 
     class T1,
Line 8: Line 8:
 
}}
 
}}
  
{{tt|std::pair}} is a struct template that provides a way to store two heterogeneous objects as a single unit. A pair is a specific case of a {{lc|std::tuple}} with two elements.
+
{{tt|std::pair}} is a class template that provides a way to store two heterogeneous objects as a single unit. A pair is a specific case of a {{lc|std::tuple}} with two elements.
  
 
If neither {{tt|T1}} nor {{tt|T2}} is a possibly cv-qualified class type with non-trivial destructor, or array thereof, the destructor of {{tt|pair}} is trivial.
 
If neither {{tt|T1}} nor {{tt|T2}} is a possibly cv-qualified class type with non-trivial destructor, or array thereof, the destructor of {{tt|pair}} is trivial.
Line 14: Line 14:
 
===Template parameters===
 
===Template parameters===
 
{{par begin}}
 
{{par begin}}
{{par | T1, T2 | the types of the elements that the pair stores.}}
+
{{par|T1, T2|the types of the elements that the pair stores.}}
 
{{par end}}
 
{{par end}}
  
 
===Member types===
 
===Member types===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc hitem | Member type | Definition}}
+
{{dsc hitem|Member type|Definition}}
{{dsc | {{tt|first_type}} | {{tt|T1}} }}
+
{{dsc|{{tt|first_type}}|{{tt|T1}}}}
{{dsc | {{tt|second_type}} | {{tt|T2}}}}
+
{{dsc|{{tt|second_type}}|{{tt|T2}}}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Member objects===
 
===Member objects===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc hitem | Member name | Type}}
+
{{dsc hitem|Member name|Type}}
{{dsc | {{tt|first}} | {{tt|T1}} }}
+
{{dsc|{{tt|first}}|{{tt|T1}}}}
{{dsc | {{tt|second}} | {{tt|T2}} }}
+
{{dsc|{{tt|second}}|{{tt|T2}}}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc mem ctor | cpp/utility/pair/pair | constructs new pair}}
+
{{dsc inc|cpp/utility/pair/dsc constructor}}
{{dsc mem fun | cpp/utility/pair/operator{{=}} | assigns the contents}}
+
{{dsc inc|cpp/utility/pair/dsc operator{{=}}}}
{{dsc mem fun | cpp/utility/pair/swap | swaps the contents | notes={{mark c++11}} }}
+
{{dsc inc|cpp/utility/pair/dsc swap}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Non-member functions===
 
===Non-member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/utility/pair/dsc make_pair}}
+
{{dsc inc|cpp/utility/pair/dsc make_pair}}
{{dsc inc | cpp/utility/pair/dsc operator_cmp}}
+
{{dsc inc|cpp/utility/pair/dsc operator_cmp}}
{{dsc inc | cpp/utility/pair/dsc swap2}}
+
{{dsc inc|cpp/utility/pair/dsc swap2}}
{{dsc inc | cpp/utility/pair/dsc get}}
+
{{dsc inc|cpp/utility/pair/dsc get}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Helper classes===
 
===Helper classes===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/utility/pair/dsc tuple_size}}
+
{{dsc inc|cpp/utility/pair/dsc tuple_size}}
{{dsc inc | cpp/utility/pair/dsc tuple_element}}
+
{{dsc inc|cpp/utility/pair/dsc tuple_element}}
 +
{{dsc inc|cpp/utility/pair/dsc basic_common_reference}}
 +
{{dsc inc|cpp/utility/pair/dsc common_type}}
 +
{{dsc inc|cpp/utility/format/dsc tuple_formatter|pair}}
 
{{dsc end}}
 
{{dsc end}}
 +
 +
===Helper specializations===
 +
{{dcl begin}}
 +
{{dcl|since=c++23|1=
 +
template< class T, class U >
 +
constexpr bool enable_nonlocking_formatter_optimization<std::pair<T, U>>
 +
= enable_nonlocking_formatter_optimization<T> &&
 +
  enable_nonlocking_formatter_optimization<U>;
 +
}}
 +
{{dcl end}}
 +
This specialization of {{ltt std|cpp/utility/format/enable_nonlocking_formatter_optimization}} enables efficient implementation of {{ltt std|cpp/io/print}} and {{ltt std|cpp/io/println}} for printing a {{tt|pair}} object when both {{tt|T}} and {{tt|U}} enable it.
  
 
==={{rl|deduction_guides|Deduction guides}}{{mark since c++17}}===
 
==={{rl|deduction_guides|Deduction guides}}{{mark since c++17}}===
Line 61: Line 75:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/utility/dsc tuple}}
+
{{dsc inc|cpp/utility/dsc tuple}}
{{dsc inc | cpp/utility/tuple/dsc tie}}
+
{{dsc inc|cpp/utility/tuple/dsc tie}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|cs|de|es|fr|it|ja|ko|pl|pt|ru|zh}}
 
{{langlinks|cs|de|es|fr|it|ja|ko|pl|pt|ru|zh}}

Latest revision as of 04:28, 24 July 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <utility>
template<

    class T1,
    class T2

> struct pair;

std::pair is a class template that provides a way to store two heterogeneous objects as a single unit. A pair is a specific case of a std::tuple with two elements.

If neither T1 nor T2 is a possibly cv-qualified class type with non-trivial destructor, or array thereof, the destructor of pair is trivial.

Contents

[edit] Template parameters

T1, T2 - the types of the elements that the pair stores.

[edit] Member types

Member type Definition
first_type T1
second_type T2

[edit] Member objects

Member name Type
first T1
second T2

[edit] Member functions

constructs new pair
(public member function) [edit]
assigns the contents
(public member function) [edit]
(C++11)
swaps the contents
(public member function) [edit]

[edit] Non-member functions

creates a pair object of type, determined by the argument types
(function template) [edit]
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)
lexicographically compares the values in the pair
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
accesses an element of a pair
(function template) [edit]

[edit] Helper classes

obtains the size of a pair
(class template specialization) [edit]
obtains the type of the elements of pair
(class template specialization) [edit]
determines the common reference type of two pairs
(class template specialization) [edit]
determines the common type of two pairs
(class template specialization) [edit]
formatting support for pair
(class template specialization) [edit]

[edit] Helper specializations

template< class T, class U >

constexpr bool enable_nonlocking_formatter_optimization<std::pair<T, U>>
 = enable_nonlocking_formatter_optimization<T> &&

   enable_nonlocking_formatter_optimization<U>;
(since C++23)

This specialization of std::enable_nonlocking_formatter_optimization enables efficient implementation of std::print and std::println for printing a pair object when both T and U enable it.

[edit] Deduction guides(since C++17)

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2796 C++98 triviality of the destructor of pair was unspecified specified

[edit] See also

(C++11)
implements fixed size container, which holds elements of possibly different types
(class template) [edit]
(C++11)
creates a tuple of lvalue references or unpacks a tuple into individual objects
(function template) [edit]