• C++——pair用法总结


    1.pair概述(在标头 定义)

    std::pair 是类模板,提供在一个单元存储两个相异类型对象的途径。简单描述,即pair可以将2个数据组合为一个数据,当然pair的返回值也可以是2个数据。

    template struct pair;
    其中,T1是first_type,T2是second_type。
    
    • 1
    • 2

    有关pair的描述,具体见下:

    描述成员1(T1)成员2(T2)
    成员类型first_typesecond_type
    成员名firstsecond

    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& p );③以 p.first 初始化 first 并以 p.second 初始化 second。
    template< class U1, class U2 >pair( const pair& p );以 p.first 初始化 first 并以 p.second 初始化 second。
    template< class U1, class U2 >pair( pair&& p );以std::forward(p.first) 初始化 first 并以 std::forward(p.second) 初始化 second。
    template< class U1, class U2 >constexpr pair( const pair&& 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& other );⑤赋值 other.first 给 first , other.second 给 second 。
    pair& operator=( pair&& other );⑥移动赋值运算符。用移动语义以 other 的内容替换内容。
    template< class U1, class U2 >pair& operator=( pair&& other );⑦赋值 std::forward(p.first) 给 first , std::forward(p.second) 给 second 。
    |
    
    • 1
    #include 
    #include 
    #include 
    
    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 };//②以1初始化first并以3初始化 second
        cout << "p2.first: " << p2.first << "   p2.second : " << p2.second << endl;
    
        pair<int, int> p3{ p2 };//③以p.first初始化first并以p.second初始化second。
        cout << "p3.first: " << p3.first << "   p3.second : " << p3.second << endl;
    
        pair<int, string> p4{ 3,"Hello World" };
        p1 = p4;//④复制赋值运算符。以 other 内容的副本替换内容。
        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);//⑦operator=( pair&& other );
        cout << "p5.first: " << p5.first  << endl;
        cout << "p6.first: " << p6.first << endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

    2.2非成员函数

    非成员函数描述
    template< class T1, class T2 >std::pair make_pair( T1 t, T2 u );构造 std::pair 对象,从参数类型推导目标类型。
    template< class T1, class T2 >bool operator==( const std::pair& lhs, const std::pair& rhs );若 lhs.first = rhs.first 且 lhs.second = rhs.second 则为 true ,否则为 false
    template< class T1, class T2 >bool operator!=( const std::pair& lhs, const std::pair& rhs );!(lhs == rhs)
    template< class T1, class T2 >bool operator<( const std::pair& lhs, const std::pair& rhs );按字典序比较 lhs 和 rhs ,即比较首元素,然后仅若它们等价,再比较第二元素。若 lhs.firstrhs.first 则返回 false 。否则,若 lhs.second
    template< class T1, class T2 >bool operator>( const std::pair& lhs, const std::pair& rhs );rhs < lhs
    template< class T1, class T2 >bool operator>=( const std::pair& lhs, const std::pair& rhs );!(lhs < rhs)
    template< class T1, class T2 >void swap( std::pair& x, std::pair& y )交换 x 与 y 的内容。等价于 x.swap(y) 。
    template constexpr T& get(std::pair& p) ;返回到 p.first 的引用。
    template constexpr const T&& get(const std::pair&& p) noexcept;返回到 p.second 的引用。
    template< std::size_t I, class T1, class T2 >constexpr std::tuple_element_t >& get( std::pair& p )若 I0 则返回到 p.first 的引用,若 I1 则返回到 p.second 的引用。
    #include 
    #include 
    
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    #include 
    #include 
    #include 
    
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述

    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main()
    {
        pair<int, int> p1{ 1,3 };
        
        int a = get<0>(p1);
        cout << a << endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

    2.3辅助类

    辅助类描述
    template struct tuple_size> : std::integral_constant { };std::tuple_size 对 pair 的部分特化提供使用类 tuple 语法,在编译时获得 pair 中元素数的方法,该数总是 2 。
    template< std::size_t I, class T1, class T2 >
    struct tuple_element>;std::tuple_element 对 pair 的部分特化使用类 tuple 语法,提供 pair 元素类型的编译时访问。

    使用

    输出pair中的所有元素

    #include 
    #include 
    #include 
    
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

  • 相关阅读:
    计算建模之EM算法
    HTTP协议
    关于使用Java-JWT的笔记
    Linux 线程池&单例模式&读写锁&自旋锁
    java计算机毕业设计校园临时用工网站MyBatis+系统+LW文档+源码+调试部署
    【操作系统】进程:哲学家进餐问题
    vue实现按需加载的多种方式
    Qt 学习(四) —— QCommandLinkButton命令链接按钮
    webpack:自定义plugin插件开发
    工业级POE交换机支持什么?
  • 原文地址:https://blog.csdn.net/qq_44423388/article/details/127886000