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++程序员提供了一个可扩展的应 ...
随机推荐
- tableView创建方法调用的研究
当两个section的cell数量都为5的时候,方法的调用顺序: -[ViewController numberOfSectionsInTableView:] -[ViewController tab ...
- jQuery中的阻止默认行为
在很多元素中都存在默认行为,例如表单中的submit按钮,a标签等等.如果想要消除其中的默认行为,就需要一个事件event的方法来消除他们的默认行为. 这个方法就是event.preventDefau ...
- sql server2008附加数据库5120错误
解决方法: 附加数据库时,显示错误,错误信息为 一种解决方法为,设置mdf文件所在文件夹的权限(有些资料说只设置mdf文件的权限就好,但我试了不管用),在文件夹上右击——属性——安全,如图所示: 选择 ...
- 在linux下将当前目录文件全部小写含目录名
ls | sed -n '/[A-Z]/s/.*/mv & \L&/e' 公司以前用的windows server 服务器 文件大小写都一样. 新迁移到centos 服务器上,发现 ...
- C# ORM—Entity Framework 之Code first(代码优先)(二)
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
- 查看memcached中最大生存时间
如果想看一下线上服务器上存储时间最久的key是多长时间,又没有memcached-tool工具可用的话,可以使用这个命令 stats items 执行结果如下: stats items :number ...
- PHP之路——Mysql多表查询
select a.id,a.`name` AS '姓名',b.`subject`,c.`achievement` from aaa AS a left join ccc AS c on a.id=c. ...
- poj 1606Jugs
http://poj.org/problem?id=1606 #include<cstdio> #include<cstring> #define MAXN 1000000 u ...
- POJSorting It All Out (拓扑)
题目链接. 题目大意: 给定一定的数量的小于关系: 1.如果发现环,输出从前几次就可以确定出现环 2.如果能够确定唯一序列,输出. 3.如果通过全部关系,还不能确定序列,则输出不能确定. 分析: 个人 ...