STL之Pairs
什么是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的更多相关文章
- codeforces 1045I Palindrome Pairs 【stl+构造】
题目:戳这里 题意:给1e5个字符串,问有多少对字符串组合,满足最多只有一种字符有奇数个. 解题思路:每种情况用map存一下就行了.感觉这题自己的代码思路比较清晰,所以写个题解记录一下 附ac代码: ...
- make_pair() (STL)
转载来的 Pairs C++标准程序库中凡是“必须返回两个值”的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和multimap就是使用pairs来管理其 ...
- STL map 用法
首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和mul ...
- STL map详细用法和make_pair函数
今天练习华为上机测试题,遇到了map的用法,看来博客http://blog.csdn.net/sprintfwater/article/details/8765034:感觉很详细,博主的其他内容也值得 ...
- C++ Templates STL标准模板库的基本概念
STL标准库包括几个重要的组件:容器.迭代器和算法.迭代器iterator,用来在一个对象群集的元素上进行遍历操作.这个对象群集或许是一个容器,或许是容器的一部分.迭代器的主要好处是,为所有的容器提供 ...
- STL学习笔记(第四章 通用工具)
本章讲解C++标准程序库中的通用工具.它们是由短小精干的类和函数构成. Pairs(对组) class pair可以将两个值视为一个单元.STL内多处使用了pair.尤其容器map和multimap, ...
- [C++ STL] 各容器简单介绍
什么是STL? 1.STL(Standard Template Library),即标准模板库,是一个高效的C++程序库. 2.包含了诸多常用的基本数据结构和基本算法.为广大C++程序员们提供了一个可 ...
- 详细解说 STL 排序(Sort)
0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...
- STL标准模板库(简介)
标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...
随机推荐
- jsp <%! %> 与 <% %> 区别
转自huangqiqing123.iteye.com/blog/1922014 <body> <%! //1.可定义方法 public String outMethod(){ ret ...
- 人民币符号¥在css和html正确显示
商城项目需要涉及到人民币的页面现实问题.但是¥(指的是通常输入法中文全角模式下按shift+4的那个)在宋体(v3.03, v5.0)的情况下是显示一杠.常见的其他字体微软雅黑(Microsoft Y ...
- angularjs学习笔记三——directive
AngularJS 通过被称为 指令 的新属性来扩展 HTML. 正如你所看到的,AngularJS 指令是以 ng 作为前缀的 HTML 属性. HTML5 允许扩展的(自制的)属性,以 data- ...
- FLEX 网格布局及响应式处理
上一篇文章用Flex实现BorderLayout,这一章我们来实现常用的网格布局和响应式处理. 首先我们定义HTML结构,主Box为grid,每项为grid-cell,下面就是我们HTML代码结构. ...
- jquery实现DIV层拖动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Rxjava+Retrofit2+Okhttp3多文件上传(服务器端代码+客户端代码)
所有代码亲测可用,如有问题,欢迎指正. 首先在ApiService接口文件中新建文件上传接口 public interface ApiService { static final String BAS ...
- STM32f103------ADC(DMA)
STM32F10x ADC 技术指标: 分辨率: 12位分辨率 LSB=Vref+ / 2^(12) 转换时间: 采样一次至少14个ADC时钟周期 ,而ADC最高时钟周期为14MHz 选用采样 ...
- UVA253 Cube painting(数学)
题目链接. 分析: 用的<训练指南>上的方法.详见P17. 从6个面中选一个做顶面,再从剩下的4个面中选1个做正面,则此正方体唯一确定. 需要枚举共6*4=24种. #include &l ...
- 【转】两分钟彻底让你明白Android Activity生命周期(图文)!----不错
原文网址:http://blog.csdn.net/android_tutor/article/details/5772285 大家好,今天给大家详解一下Android中Activity的生命周期,我 ...
- DatabaseMetaData的用法【转载】
一 . 得到这个对象的实例Connection con ;con = DriverManager.getConnection(url,userName,password);DatabaseMetaDa ...