C++ Utility std::pair template
The C++ std::pair templates consist of only two data members.And these two objects are treated as a pair.The types of the objects can be same or different.The std::pair template is defined under <utility> header.
templates declaration
namespace std {
template<typename T1, typename T2>
struct pair;
}
Data members
Note::They belong to public type.
T1 first; | //name of the first data member |
---|---|
T2 second; | //name of the second data-member |
Constructors and copy-constructor
Note:they are all templates.
pair(); |
---|
explicit constexpr pair(); |
constexpr pair(const T1& a, const T2& b); |
explicit constexpr pair(const T1& a, const T2& b) |
constexpr pair(const pair< Tp1, Tp2>& p); |
explicit constexpr pair(const pair<Tp1, Tp2>& p); |
constexpr pair(const pair&) = default; |
constexpr pair(pair&&) = default; |
constexpr pair( Tp1&& x, const T2& y); |
explicit constexpr pair(Tp1&& x, const T2& y); |
constexpr pair(const T1& x, Tp2&& y); |
explicit pair(const T1& x, Tp2&& y); |
constexpr pair(Tp1&& x, Tp2&& y); |
explicit constexpr pair(Tp1&& x, Tp2&& y); |
constexpr pair(pair<Tp1, Tp2>&& p); |
explicit constexpr pair(pair<Tp1, Tp2>&& p); |
Member functions
Return type |
Function name and argument |
Function definition |
---|---|---|
Operator assignment |
||
pair& | operator=(const pair& p) (There are many variant of this function) |
Assign one pair object to another. |
swaping function |
||
void | swap(pair& p) | Swap the data member of the two pair objects. |
Non-member functions
Operators functions |
||
---|---|---|
template<typename T1, typename T2> inline bool | operator==(const pair<T1, T2>& x, const pair<T1, T2>& y) | Compare two pair objects for equality. |
template<typename T1, typename T2> inline bool | operator<(const pair<T1, T2>& x, const pair<T1, T2>& y) | Compare if one object is smaller than the other. |
template<typename T1, typename T2> inline bool | operator!=(const pair<T1, T2>& x, const pair<T1, T2>& y) | Compare two pair object for not equality. |
template<typename T1, typename T2> inline bool | operator>(const pair<T1, T2>& x, const pair<T1, T2>& y) | Compare if one object is greater than the other. |
template<typename T1, typename T2> inline bool | operator<=(const pair<T1, T2>& x, const pair<T1, T2>& y) | Compare two pair objects for smaller than or equality. |
template<typename T1, typename T2> inline bool | operator>=(const pair<T1, T2>& x, const pair<T1, T2>& y) | Compare two pair objects for greater than or equality. |
make_pair and swap functions |
||
template<typename T1, typename T2>inline void | swap(pair< T1, T2> & x, pair<T1, T2>& y) | Swap the data members of the two pair object. |
template<typename T1, typename T2> constexpr pair< T1, T2> |
make_pair(T1&& x, T2&& y) | Make a pair object out of two values passed as arguments. |
There is also another ‘swap‘ function that accept various types of data,not just pair objects.
Code example
#include &lt;iostream&gt; #include &lt;utility&gt; using namespace std ; int main( ) { pair&lt;int , string&gt; pr{ 45 , “Pair string” } , pr1={23 , “New pair string”}; ///work fine cout&lt;&lt; pr.first &lt;&lt; endl ///gives the first data &lt;&lt; pr.second ; ///gives the second data pair&lt;int , string&gt; pr1{ 46 , “Pair string” }; cout&lt;&lt; (pr!=pr1) ; //give true pair&lt;double, string&gt; prDs={ “23.45” , “1290” } ; ///error,type does not match cin.get( ); return 0 ; }
Output
45
Pair string
1