什么是Pair

  关于类Pair的介绍,下面是引自《C++ Standard Library》的一段话:

  The class pair is provided to treat two values as a single unit. It is used in several places within the C++ standard library. In particular, the container classes map and multimap use pairs to manage their elements, which are key/value pairs (See Section 6.6). Another example of the usage of pairs is functions that return two values.

Pair的定义

  The structure pair is defined in <utility> as follows:

namespace std
{
template <class T1, class T2>
struct pair
{
//type names for the values
typedef T1 first_type;
typedef T2 second_type; //member
T1 first;
T2 second; /* default constructor
* - T1 () and T2 () force initialization for built-in types
*/
pair(): first(T1()), second(T2())
{
} //constructor for two values
pair(const T1& a, const T2& b): first(a), second(b)
{
} //copy constructor with implicit conversions
template<class U, class V>
pair(const pair<U,V>& p): first(p.first), second(p.second)
{
}
}; //comparisons
template <class T1, class T2>
bool operator== (const pair<T1,T2>&, const pair<T1,T2>&); template <class T1, class T2>
bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); //similar: !=, <=, >, >= //convenience function to create a pair
template <class T1, class T2>
pair<T1,T2> make_pair (const T1&, const T2&);
} namespace std {
//comparisons
template <class T1, class T2>
bool operator== (const pair<T1,T2>& x, const pair<T1,T2>& y)
{
return x.first == y.first && x.second == y.second;
} //similar: !=, <=, >, >= template <class T1, class T2>
bool operator< (const pair<T1,T2>& x, const pair<T1,T2>& y)
{
return x.first < y.first || (!(y.first < x.first) && x.second < y.second);
} //create value pair only by providing the values
template <class T1, class T2>
pair<Tl,T2> make_pair (const T1& x, const T2& y) {
return pair<T1,T2>(x, y);
}
}

Pair的使用

std::pair<int,float> p;    //initialize p. first and p.second with zero

void f(std::pair<int,const char*>);
void g(std::pair<const int.std::string>);
void foo
{
std::pair<int,const char*> p(,"hello");
f(p); //OK: calls built-in default copy constructor
g(p); //OK: calls template constructor
} std::make_pair(, '@'); //or
std::pair<int,char>(,'@'); void f(std::pair<int,const char*>);
void g(std::pair<const int,std::string>); void foo {
f(std::make_pair(,"hello")); //pass two values as pair
g(std::make_pair(,"hello")); //pass two values as pair
// with type conversions
} std::pair<int,float>(,7.77);
//does not yield the same as
std::make_pair(,7.77);

  The C++ standard library uses pairs a lot.

  For example, the map and multimap containers use pair as a type to manage their elements, which are key/value pairs. See Section 6.6, for a general description of maps and multimaps, and in particular page 91 for an example that shows the usage of type pair. Objects of type pair are also used inside the C++ standard library in functions that return two values (see page 183 for an example).

STL之Pairs的更多相关文章

  1. codeforces 1045I Palindrome Pairs 【stl+构造】

    题目:戳这里 题意:给1e5个字符串,问有多少对字符串组合,满足最多只有一种字符有奇数个. 解题思路:每种情况用map存一下就行了.感觉这题自己的代码思路比较清晰,所以写个题解记录一下 附ac代码: ...

  2. make_pair() (STL)

    转载来的 Pairs C++标准程序库中凡是“必须返回两个值”的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和multimap就是使用pairs来管理其 ...

  3. STL map 用法

    首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象  class pair可以将两个值视为一个单元.容器类别map和mul ...

  4. STL map详细用法和make_pair函数

    今天练习华为上机测试题,遇到了map的用法,看来博客http://blog.csdn.net/sprintfwater/article/details/8765034:感觉很详细,博主的其他内容也值得 ...

  5. C++ Templates STL标准模板库的基本概念

    STL标准库包括几个重要的组件:容器.迭代器和算法.迭代器iterator,用来在一个对象群集的元素上进行遍历操作.这个对象群集或许是一个容器,或许是容器的一部分.迭代器的主要好处是,为所有的容器提供 ...

  6. STL学习笔记(第四章 通用工具)

    本章讲解C++标准程序库中的通用工具.它们是由短小精干的类和函数构成. Pairs(对组) class pair可以将两个值视为一个单元.STL内多处使用了pair.尤其容器map和multimap, ...

  7. [C++ STL] 各容器简单介绍

    什么是STL? 1.STL(Standard Template Library),即标准模板库,是一个高效的C++程序库. 2.包含了诸多常用的基本数据结构和基本算法.为广大C++程序员们提供了一个可 ...

  8. 详细解说 STL 排序(Sort)

    0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...

  9. STL标准模板库(简介)

    标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...

随机推荐

  1. iOS 上传项目常见问题

    一. Archive项目 时,出现"Your build settings specify a provisioning profile with the UUID "XXX&qu ...

  2. UICollectionView出现the behavior of the UICollectionViewFlowLayout is not defined because:

    2015-01-28 21:55:17.790 Demo[636:9351] the behavior of the UICollectionViewFlowLayout is notdefined ...

  3. JS中区分参数方法

    实现功能:在使用cocosjs制作游戏过程中,很多东西都可以重复使用,例如菜单栏等等.今天尝试写了一个自定义的Js文件用作菜单方便以后使用. 将菜单按钮,以及触发事件作为参数生成一个层 直接在游戏中使 ...

  4. uvalive 5721 Activation (概率dp+方程)

    题目链接:http://vjudge.net/problem/viewProblem.action?id=24999 主要思想就是解方程的思想. 二维dp应该很容易想到,就是当前位置加队伍长度. dp ...

  5. 基于VC的串行通信技术应用实例

    在工业控制中,串口是常用的计算机与外部串行设备之间的数据传输通道,由于串行通信方便易行,所以应用广泛.   本文将介绍在Windows平台下串行通信的工作机制和用Visual C++设计串行通信程序的 ...

  6. Ajax的load方法演示

    load方法的参数形式为: load(url,[data],[callback]); 其中url为请求HTML页面的URL地址.[data]表示发送至服务器的key/value数据.callback表 ...

  7. Groovy创建和解析json

    正文:  在Groovy 1.8发布新闻中,提到Groovy增加了对JSON的支持.Dustin Marx在其博文中,讲述了这一功能的使用. 用法真的很简单,创建一个JSON对象: import gr ...

  8. codevs 1153 道路游戏

    传送门   题目描述 Description 小新正在玩一个简单的电脑游戏.游戏中有一条环形马路,马路上有n 个机器人工厂,两个相邻机器人工厂之间由一小段马路连接.小新以某个机器人工厂为起点,按顺时针 ...

  9. mschedule 简单linux进程管理(树莓派)

    树莓派是神奇的机器,CPU和内存都少的可怜,但体积小功耗低,在上面搞些动搞些西其实也挺有意思,挺好玩的.装的是pidara,基本服务没有精简多少,先cat一下CPU和RAM. [able@raspi ...

  10. UML问题

    1.在创建协作图时需要先确定参与者,而协作图的工具栏里是没有Actor的,这是需要先new Actor,然后把其拖动到工作区:实验过程中发现必须创建在用例视图下,若是创建在逻辑试图下那么根本无法继续操 ...