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++程序员提供了一个可扩展的应 ...
随机推荐
- 10个加速Table Views开发的Tips(转)
本文由CocoaChina译者yake_099(博客)翻译,作者:David McGraw原文:10 Actionable Performance Tips To Speed Up Your Tabl ...
- Linux2.6的所有内核版本
Index of /pub/linux/kernel/v2.6 Name Last modified Size Parent Directory - incr/ 03-Aug-2011 20:47 - ...
- 一种实现C++反射功能的想法(一)
Java的反射机制很酷, 只需知道类的名字就能够加载调用. 这个功能很实用, 想象一下, 用户只需指定类的名称, 就可以动态绑定类型, 而且只需通过字符串指定, 字符串的使用可以使得用户的修改只需修改 ...
- c#结构体和字节数组的转换、字节数组和stream的转换
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/streambytsstruct.html using System; using ...
- Cocos2dx 3.2 节点之间相互通信与设置触摸吞噬的方法
实际开发中,我们经常会遇到这样的情况.我们有一个层layer1,这个层包含一个menu层,menu1层里又包含了一个节点按钮button1.现在需要实现一个效果:点击button1弹出一个对话框,这个 ...
- grails通过findBy或findBy查找的结果集进行排序
原文:http://grails.org/doc/2.3.x/ref/Domain%20Classes/list.html list Purpose Lists instances of the do ...
- PHP引用(&)详解
PHP的引用(就是变量.函数.对象等前面加上&符号) 在PHP 中引用的意思是:不同的名字访问同一个变量内容. 变量的引用 PHP 的引用允许你用两个变量来指向同一个内容 //打印数组 fun ...
- table 表格隔行换色实现
table 表格隔行换色实现 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...
- iOS开发——OC篇&纯代码退出键盘
关于iOS开发中键盘的退出,其实方法有很多中,而且笔者也也学会了不少,包括各种非纯代码界面的退出. 但是最近开始着手项目的时候却闷了,因为太多了,笔者确实知道有很多中方法能实现,而且令我影响最深的就是 ...
- es6整理
1.const和let let类似于var,不同的是let只在所在的代码段有效 for循环的计数器,就很合适使用let命令. let和var的区别: //变量i是var声明的,在全局范围内都有效.所以 ...