Pair模版是C++ STL中短小精干的结构体,被定义在<utility>头文件中,把两个值视为一个结构单元,如map和multimap都利用了很多pair对组模版,即<键/值>为成对元素,另外函数需要返回两个值的时候,也常用pair模版(当然,结构体也一样方便)。
注意:pair被定义为结构体而不是类,这样一来他的成员都是公有属性,可以直接访问和赋值。
Pair的定义:
std::pair<int,std::string> p(3,“yaoyao”);(C++ 所有标准都是定义在std名称空间中的)
这样一来可以通过p.first访问3,p.second访问”yaoyao”。
Pair的比较:
两个pair类型的变量也可以进行比较大小,但是他们的比较具有优先性,也就是当first与first比较,如果可以比较出结果时,就停止比较,以first比较结果为准,否则才进行second的比较。
例:std::pair<int,std::string> p1(5,”abc”),p2(4.”abc”),p3(5.”abb”);
比较可知 p1>p2 p1<p3 p2<p3.
当且仅当first与second都相等时,他们才相等。
便捷函数make_pair():该函数也可以进行两个成员的代替,好处是不需要指明成员变量类型。使用起来更方便快捷。
例:
用pair表示: std::pair<int,std::string> p(5,”yaoyao”); 将p插入到容器中insert(p);
用make_pair: insert ( make_pair(5,”yaoyao”) );
通过比较可以看到使用make_pair不需要中间变量和类型说明,比较方便。
当然也不是以后都使用make_pair,有些地方需要确切的指明类型,例如对于float的3.14,make_pair会把它当成double处理。所以某些情况下也会带来一定的麻烦。
Pair运用实例:
因为pair始终与map,所以实例见以后的map。学习map前的铺垫先做一下。到这儿的介绍完全盗自http://blog.csdn.net/effective_coder/article/details/8733645
====================================================================================
下面是stl_pair.h的源码:
// Filename: stl_pair.h // Comment By: 凝霜 // E-mail: [email protected] // Blog: http://blog.csdn.net/mdl13412 /* * * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. * * * Copyright (c) 1996,1997 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Silicon Graphics makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. */ /* NOTE: This is an internal header file, included by other STL headers. * You should not attempt to use it directly. */ #ifndef __SGI_STL_INTERNAL_PAIR_H #define __SGI_STL_INTERNAL_PAIR_H __STL_BEGIN_NAMESPACE // pair只是一个wraper, 所以要提供最佳效率 // 使用struct的原因是我们要能方便的存取内部元素 // pair在关联式容器中的使用极为广泛, 其本身也可以嵌套使用 template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair() : first(T1()), second(T2()) {} pair(const T1& a, const T2& b) : first(a), second(b) {} // 此版本并未提供operator =()的支持, 个人认为应该提供 #ifdef __STL_MEMBER_TEMPLATES // 允许使用兼容的pair进行复制构造 template <class U1, class U2> pair(const pair<U1, U2>& p) : first(p.first), second(p.second) {} #endif }; // 只有当pair中的两个成员均相等时, 才判定两个pair相等 // 使用自定义类型时最好提供operator ==重载 template <class T1, class T2> inline bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y) { return x.first == y.first && x.second == y.second; } // 连个pair进行比较操作时, 以第一个元素为主, 如果第一个元素不能决定表达式的值 // 那么再进行第二个元素的比较 // 使用自定义类型时最好提供operator <重载 template <class T1, class T2> inline 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); } // 至于为什么没有提供operator !=, >, >=, <= // 这个是因为其在<stl_relops.h>中有实现, 其只依赖operator <和== // 所以在此特化operator ==, <就能满足要求 // 提供<stl_relops.h>的作用是如果需要特化operator XXX // 那么我们仅需要特化operator ==和<即可同时重载所有operator // 这里使用了RVO(Return Value Optimization)机制, 如果编译器支持, // 则可以消除临时对象的构造和析构负担 // 详细细节见<Inside The C++ Object Model> template <class T1, class T2> inline pair<T1, T2> make_pair(const T1& x, const T2& y) { return pair<T1, T2>(x, y); } __STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_PAIR_H */ // Local Variables: // mode:C++ // End: