std::pair

一、

std::pair可以将两个value值组成一个单元,经常用于map、multimap、unordered_map、
unordered_multimap,以及函数需要返回一对值时使用。
std::pair是一个struct而不是一个class,其所有成员均为public:
二、

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的例子程序:
注意:使用pair与make_pair要包含头文件 #include  < utility >

    #include <iostream>
     
    #include <utility>
    #include <string>
    using namespace std;
     
     
    int main ()
    {
        pair <string,double> product1 ("tomatoes",3.25);
        pair <string,double> product2;
        pair <string,double> product3;
     
        product2.first = "lightbulbs"; // type of first is string
        product2.second = 0.99; // type of second is double
        product3 = make_pair ("shoes",20.0);
     
        cout << "The price of " << product1.first << " is $" << product1.second << "\n";
        cout << "The price of " << product2.first << " is $" << product2.second << "\n";
        cout << "The price of " << product3.first << " is $" << product3.second << "\n";
        return 0;
    }

输出的结果是:

The price of tomatoes is $3.25

The price of lightbulbs is $0.99

1 pair的应用

pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存。另一个应用是,当一个函数需要返回2个数据的时候,可以选择pair。 pair的实现是一个结构体,主要的两个成员变量是first second 因为是使用struct不是class,所以可以直接使用pair的成员变量。

    int parse_nodes(std::vector<std::pair<std::string, uint16_t> >* nodes, const std::string& nodes_string)
    {
        std::string::size_type len = 0;
        std::string::size_type pos = 0;
        std::string::size_type comma_pos = 0;
     
        while (comma_pos != std::string::npos)
        {
            comma_pos = nodes_string.find(',', pos);
            if (comma_pos != std::string::npos)
                len = comma_pos - pos;
            else
                len = nodes_string.size() - comma_pos;
     
            if (len > 0)
            {
                const std::string& str = nodes_string.substr(pos, len);
                const std::string::size_type colon_pos = str.find(':');
                if (colon_pos != std::string::npos)
                {
                    const std::string& ip_str = str.substr(0, colon_pos);        //IP地址    
                    const std::string& port_str = str.substr(colon_pos + 1);    //端口号
                    nodes->push_back(std::make_pair(ip_str, (uint16_t)atoi(port_str.c_str())));
                }
            }
     
            pos = comma_pos + 1; // Next node
        }
     
        return static_cast<int>(nodes->size());
    }

2 make_pair函数

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

很明显,我们可以使用pair的构造函数也可以使用make_pair来生成我们需要的pair。 一般make_pair都使用在需要pair做参数的位置,可以直接调用make_pair生成pair对象很方便,代码也很清晰。 另一个使用的方面就是pair可以接受隐式的类型转换,这样可以获得更高的灵活度。灵活度也带来了一些问题如:

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

std::make_pair(1, 1.1);

是不同的,第一个就是float,而第2个会自己匹配成double。

类模板:template <class T1, class T2> struct pair

参数:T1是第一个值的数据类型,T2是第二个值的数据类型。

功能:pair将一对值组合成一个值,这一对值可以具有不同的数据类型(T1和T2),两个值可以分别用pair的两个公有函数first和second访问。

具体用法:

1.定义(构造):

1     pair<int, double> p1;  //使用默认构造函数
2     pair<int, double> p2(1, 2.4);  //用给定值初始化
3     pair<int, double> p3(p2);  //拷贝构造函数

2.访问两个元素(通过first和second):

1     pair<int, double> p1;  //使用默认构造函数
2     p1.first = 1;
3     p1.second = 2.5;
4     cout << p1.first << ' ' << p1.second << endl;

输出结果:1 2.5

3.赋值operator = :

(1)利用make_pair:

1     pair<int, double> p1;
2     p1 = make_pair(1, 1.2);

(2)变量间赋值:

   pair<int, double> p1(1, 1.2);

  pair<int, double> p2 = p1;

Pair类型概述

pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下:

pair<int, string> a;

表示a中有两个类型,第一个元素是int型的,第二个元素是string类型的,如果创建pair的时候没有对其进行初始化,则调用默认构造函数对其初始化。

pair<string, string> a("James", "Joy");

也可以像上面一样在定义的时候直接对其初始化。

由于pair类型的使用比较繁琐,因为如果要定义多个形同的pair类型的时候,可以时候typedef简化声明:

typedef pair<string, string> author;

author pro("May", "Lily");

author joye("James", "Joyce");

Pair对象的操作

    对于pair类,由于它只有两个元素,分别名为first和second,因此直接使用普通的点操作符即可访问其成员

pair<string, string> a("Lily", "Poly");

string name;

name = pair.second;

    生成新的pair对象

可以使用make_pair对已存在的两个数据构造一个新的pair类型:

int a = 8;

string m = "James";

pair<int, string> newone;

newone = make_pair(a, m);
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值