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++程序员提供了一个可扩展的应 ...
随机推荐
- iOS: TableView如何刷新指定的cell 或section
//一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2]; [tableview reloadSections:in ...
- PHP MySQL 预处理语句
PHP MySQL 预处理语句 预处理语句对于防止 MySQL 注入是非常有用的. 预处理语句及绑定参数 预处理语句用于执行多个相同的 SQL 语句,并且执行效率更高. 预处理语句的工作原理如下: 预 ...
- 分享一个md5类
这个md5干嘛用的,大家比我清楚就不说了,这里不是讲md5的原理.要讲md5的原理,网上一大堆,我也不是什么算法很厉害的人,我只是算法搬运工.咱是一般程序员,有时候能完成业务需要就可以,那些伟大算法的 ...
- hdu1142 A Walk Through the Forest( Dijkstra算法+搜索)
看到这道题,想起了我家旁边的山! 那是一座叫做洪山寨的山,据说由当年洪秀全的小妾居住于此而得名! 山上盛产野果(很美味)! 好久没有爬上去了! #include<stdio.h> #inc ...
- Best Time to Buy and Sell sock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- jquery 插件大全
1.jquery.roundabout.js 超棒的左右3D旋转式幻灯片jQuery插件 2.jquery validate.js 验证表单 3.jquery ui插件 对话框 日期 4.lhgdia ...
- 百度地图api窗口信息自定义
百度地图加载完后,完全可以用dom方法操作,比较常用的就是点击mark的弹窗,利用jQuery可以很快的创建弹窗,需要注意的就是地图都是异步加载,所以绑定时间要用 jQuery 事件 - delega ...
- [Python笔记]第九篇:re正则表达式
一.正则表达式基础 1.正则表达式介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十分 ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- InstallShield FEQ
Q: 如何替换setup.exe的图标? A: 这不是一个推荐的操作,因为可能会引起不可预见的错误,而且IS没有开放这个接口.如果你坚持要这么做,可以使用第三方软件比如ExeScope来进行图标替换. ...