C++——pair用法总结
1.pair概述(在标头 <utility> 定义) 2.pair使用 2.1成员函数(构造函数、赋值函数) 2.2非成员函数
2.3辅助类 使用
1.pair概述(在标头 定义)
std::pair 是类模板,提供在一个单元存储两个相异类型对象的途径。简单描述,即pair可以将2个数据组合为一个数据,当然pair的返回值也可以是2个数据。
template<class T1, class T2 > struct pair;
其中,T1是first_type,T2是second_type。
有关pair的描述,具体见下:
描述 成员1(T1) 成员2(T2) 成员类型 first_type second_type 成员名 first second
2.pair使用
2.1成员函数(构造函数、赋值函数)
构造函数 描述 pair ();①默认构造函数 。值初始化 pair 的两个元素 first 和 second。 pair ( const T1& x, const T2& y );②以 x 初始化 first 并以 y 初始化 second。 template< class U1, class U2 >pair ( U1&& x, U2&& y ); 以 std::forward(x) 初始化 first 并以 std::forward(y) 初始化 second。 template< class U1, class U2 >constexpr pair ( pair<U1, U2>& p ); ③以 p.first 初始化 first 并以 p.second 初始化 second。 template< class U1, class U2 >pair ( const pair<U1, U2>& p ); 以 p.first 初始化 first 并以 p.second 初始化 second。 template< class U1, class U2 >pair ( pair<U1, U2>&& p ); 以std::forward(p.first) 初始化 first 并以 std::forward(p.second) 初始化 second。 template< class U1, class U2 >constexpr pair ( const pair<U1, U2>&& p ); 以 std::forward(p.first) 初始化 first 并以 std::forward(p.second) 初始化 second。 pair& operator= ( const pair& other ); ④复制赋值运算符。以 other 内容的副本替换内容。 template< class U1, class U2 >pair& operator= ( const pair<U1, U2>& other ); ⑤赋值 other.first 给 first , other.second 给 second 。 pair& operator=( pair&& other ); ⑥移动赋值运算符。用移动语义以 other 的内容替换内容。 template< class U1, class U2 >pair& operator= ( pair<U1, U2>&& other ); ⑦赋值 std::forward(p.first) 给 first , std::forward(p.second) 给 second 。
|
# include <iostream>
# include <utility>
# include <vector>
using namespace std;
int main ( )
{
pair< int , string> p1;
cout << "p1.first: " << p1. first << " p1.second : " << p1. second << endl;
pair< int , int > p2{ 1 , 3 } ;
cout << "p2.first: " << p2. first << " p2.second : " << p2. second << endl;
pair< int , int > p3{ p2 } ;
cout << "p3.first: " << p3. first << " p3.second : " << p3. second << endl;
pair< int , string> p4{ 3 , "Hello World" } ;
p1 = p4;
cout << "p1.first: " << p1. first << " p1.second : " << p1. second << endl;
pair< int , vector< int >> p5{ 1 , { 1 , 3 , 5 } } , p6{ 2 , { 2 , 4 , 6 } } ;
p5 = move ( p6) ;
cout << "p5.first: " << p5. first << endl;
cout << "p6.first: " << p6. first << endl;
return 0 ;
}
2.2非成员函数
非成员函数 描述 template< class T1, class T2 >std::pair<T1, T2> make_pair ( T1 t, T2 u ); 构造 std::pair 对象,从参数类型推导目标类型。 template< class T1, class T2 >bool operator== ( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs ); 若 lhs.first = rhs.first 且 lhs.second = rhs.second 则为 true ,否则为 false template< class T1, class T2 >bool operator!= ( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs ); !(lhs == rhs) template< class T1, class T2 >bool operator< ( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs ); 按字典序比较 lhs 和 rhs ,即比较首元素,然后仅若它们等价,再比较第二元素。若 lhs.first<rhs.first 则返回 true 。否则,若 lhs.first>rhs.first 则返回 false 。否则,若 lhs.second<rhs.second 则返回 true 。否则返回 false 。 template< class T1, class T2 >bool operator> ( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs ); rhs < lhs template< class T1, class T2 >bool operator>= ( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs ); !(lhs < rhs) template< class T1, class T2 >void swap ( std::pair<T1,T2>& x, std::pair<T1,T2>& y ) 交换 x 与 y 的内容。等价于 x.swap(y) 。 template <class T, class U>constexpr T& get (std::pair<T, U>& p) ; 返回到 p.first 的引用。 template <class T, class U>constexpr const T&& get (const std::pair<U, T>&& p) noexcept; 返回到 p.second 的引用。 template< std::size_t I, class T1, class T2 >constexpr std::tuple_element_t<I, std::pair<T1,T2> >& get ( std::pair<T1, T2>& p ) 若 I0 则返回到 p.first 的引用,若 I 1 则返回到 p.second 的引用。
# include <iostream>
# include <utility>
using namespace std;
int main ( )
{
pair< int , string> p1;
p1 = make_pair ( 1 , "Hello" ) ;
cout << "p1.first: " << p1. first << " p1.second : " << p1. second << endl;
return 0 ;
}
# include <iostream>
# include <utility>
# include <vector>
using namespace std;
int main ( )
{
pair< int , int > p1{ 1 , 3 } ;
pair< int , int > p2{ 1 , 3 } ;
pair< int , int > p3{ 2 , 3 } ;
pair< int , int > p4{ 0 , 3 } ;
pair< int , int > p5{ 0 , 0 } ;
pair< int , int > p6{ 2 , 3 } ;
cout << ( p1 == p2) << endl;
cout << ( p1 < p3) << endl;
cout << ( p1 > p2) << endl;
cout << ( p1 != p2) << endl;
cout << ( p1 <= p2) << endl;
return 0 ;
}
# include <iostream>
# include <utility>
# include <vector>
using namespace std;
int main ( )
{
pair< int , int > p1{ 1 , 3 } ;
int a = get < 0 > ( p1) ;
cout << a << endl;
return 0 ;
}
2.3辅助类
辅助类 描述 template <class T1, class T2>struct tuple_size <std::pair<T1, T2>> : std::integral_constant<std::size_t, 2> { }; std::tuple_size 对 pair 的部分特化提供使用类 tuple 语法,在编译时获得 pair 中元素数的方法,该数总是 2 。 template< std::size_t I, class T1, class T2 > struct tuple_element <I, std::pair<T1, T2>>; std::tuple_element 对 pair 的部分特化使用类 tuple 语法,提供 pair 元素类型的编译时访问。
使用
输出pair中的所有元素
# include <iostream>
# include <utility>
# include <vector>
using namespace std;
int main ( )
{
pair< int , int > p1[ ] { { 1 , 3 } , { 2 , 5 } , { 3 , 7 } } ;
for ( const auto & [ value, num] : p1)
{
cout << value << " " << num << endl;
}
return 0 ;
}