zoukankan      html  css  js  c++  java
  • std::pair 和 std::make_pair

    std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型。例如std::pair<int,float> 或者 std::pair<double,double>等。pair实质上是一个结构体,其主要的两个成员变量是first和second,这两个变量可以直接使用。初始化一个pair可以使用构造函数,也可以使用std::make_pair函数,make_pair函数的定义如下:

    template pair make_pair(T1 a, T2 b) { return pair(a, b); }

    一般make_pair都使用在需要pair做参数的位置,可以直接调用make_pair生成pair对象。 另一个使用的方面就是pair可以接受隐式的类型转换,这样可以获得更高的灵活度。但是这样会出现如下问题:例如有如下两个定义:

    std::pair<int, float>(1, 1.1);
    
    std::make_pair(1, 1.1);

    其中第一个的second变量是float类型,而make_pair函数会将second变量都转换成double类型。这个问题在编程是需要引起注意。下面是一段pair与make_pair的例子程序: 

     1 #include <iostream>
     2 #include <utility>
     3 #include <string>
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     pair <string,double> produce1("tomatoes",3.25);
     9     pair <string ,double> produce2;
    10     pair <string ,double> produce3;
    11     
    12     produce2.first = "lightbulbs";
    13     produce2.second = 0.99;
    14     
    15     produce3 = make_pair("shoes",20.0);
    16     
    17     cout<<"The price of"<<produce1.first<<"is $"<<produce.second<<endl;
    18     cout<<"The price of"<<produce2.first<<"is $"<<produce.second<<endl;
    19     cout<<"The price of"<<produce3.first<<"is $"<<produce.second<<endl;
    20     
    21     return 0;
    22 }

    其运行结果如下:

    1 The price of tomatoes is $3.25
    2 The price of lightbulbs is $0.99
    3 The price of shoes is $20

    为了更好的了解pair与make_pair的机制,下面是其定义:

     1 // TEMPLATE STRUCT pair
     2 template<class _Ty1,class _Ty2> struct pair
     3 {   // store a pair of values
     4     typedef pair<_Ty1, _Ty2> _Myt;
     5     typedef _Ty1 first_type;
     6     typedef _Ty2 second_type;
     7 
     8     pair(): first(_Ty1()), second(_Ty2())
     9         {    // construct from defaults
    10         }
    11 
    12     pair(const _Ty1& _Val1, const _Ty2& _Val2): first(_Val1), second(_Val2)
    13         {    // construct from specified values
    14         }
    15 
    16     template<class _Other1,
    17         class _Other2>
    18         pair(const pair<_Other1, _Other2>& _Right)
    19         : first(_Right.first), second(_Right.second)
    20         {    // construct from compatible pair
    21         }
    22 
    23     void swap(_Myt& _Right)
    24         {    // exchange contents with _Right
    25         std::swap(first, _Right.first);
    26         std::swap(second, _Right.second);
    27         }
    28 
    29     _Ty1 first;    // the first stored value
    30     _Ty2 second;    // the second stored value
    31     };
    32 
    33 
    34 template<class _Ty1,class _Ty2> inline
    35     pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2)
    36     {    // return pair composed from arguments
    37     return (pair<_Ty1, _Ty2>(_Val1, _Val2));
    38     }

    钦此。。。

  • 相关阅读:
    mvc 数据验证金钱格式decimal格式验证
    VMware与CentOS的安装与Linux简单指令
    rbac组件引用
    Django--CRM--菜单展示, 删除合并, 权限展示
    Django--CRM--菜单排序等
    Django--CRM--一级, 二级 菜单表
    Django--权限信息操作
    Django--CRM--modelformset的用法
    Django--CRM--QueryDict, 模糊搜索, 加行级锁
    Django--CRM-客户列表展示, 分页
  • 原文地址:https://www.cnblogs.com/foundwant/p/2998058.html
Copyright © 2011-2022 走看看