• c++ pair类型


    pair的应用

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

    pair的定义 (头文件为utility)

    template <class T1, class T2> struct pair;
    Pair of values

    This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through the public members first and second

     

    pair的初始化

      pair<T1,T2> p1; create an empty pair with two elems of types t1 an t2.the elem are value_initialized.

      pair<T1,T2> p1(v1,v2)

    想要定义多个pair类型时,可以用typedef简化

    typedef pair<string,string> Author;

    Author proust("marcel","prouse");

    Author joyce("james","joy");

    pair的相关操作

    make_pair(v1,v2)    create a new pair from the values v1,v2 . the type of the pair is inferred from the typed of v1

                                   and v2

    p.first            return the public data member of p named first

    p.second 

    p1<p2          less than between two pair objects .less than is defined as dictionary ordering.return true if p1.first<

                        p2.first or if !(p2.first<p1.first)&&p1.second<p2.second. 

    pair<string,string> next_auth;

    string first,last;

    while(cin>first>>last)

    {

      //generate a pair from first an last

          next_auth=make_pair(first,last);

    }

    因为pair的数据成员是public,可以直接这么做:

    pair<string,string> next_auth;

    while(cin>>next_auth.first>>next_auth.second)

    {

    }

  • 相关阅读:
    opencv-learnopencv-Facial Landmark Detection
    【OpenCV】Learn OpenCV
    leetcode-1-TwoNums
    【c++基础】判断是否到文件末尾-eof函数
    【c++基础】int转string自动补零
    【linux】如何退出shell终端
    【机器学习算法】bagging算法
    【机器学习算法】Boostrapping算法
    【python基础】如何注释代码块
    【机器学习算法】cascade classifier级联分类器
  • 原文地址:https://www.cnblogs.com/youxin/p/2453297.html
Copyright © 2020-2023  润新知