1、简介
class pair ,中文译为对组,可以个值视为将两一对于个单元。map和multimap,是就用pairs来管理value/key的成对元素。任何函数需要回传两个值,也需要pair。
该函数的相关内容如下所示:
|->Type----->struct
|->Include---> <utility>
|->Define----> pair<calss first,calss second>(first,second)
|
|->member
| |------>first
| |------>second
|
|->Sub
| |------>constructor(default,assignment,copy)
|
|->Fun
|------>operator(==,<,<=,>,>=,!=,=)
|------>make_pair(first,second) 返回一个新的pair
需求:
Header: <utility>
Namespace: std
2、stl源码
pair的源码
// TEMPLATE STRUCT pair
template<class _Ty1, class _Ty2>
struct pair {// store a pair of values
typedef pair<_Ty1, _Ty2> _Myt;
typedef _Ty1 first_type;
typedef _Ty2 second_type;
pair()
: first(_Ty1()), second(_Ty2())
{// construct from defaults
}
pair(const _Ty1& _Val1, const _Ty2& _Val2)
: first(_Val1), second(_Val2)
{// construct from specified values
}
template<class _Other1, class _Other2>
pair(const pair<_Other1, _Other2>& _Right)
: first(_Right.first), second(_Right.second)
{// construct from compatible pair
}
void swap(_Myt& _Right)
{// exchange contents with _Right
if (this != &_Right)
{// different, worth swapping
std::swap(first, _Right.first);
std::swap(second, _Right.second);
}
}
_Ty1 first;// the first stored value
_Ty2 second;// the second stored value
};
make_pair的源码
template<class _Ty1, class _Ty2>
inline pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2)
{
// return pair composed from arguments
return (pair<_Ty1, _Ty2>(_Val1, _Val2));
}
3、使用范例
#include <utility>
#include <stdio.h>
using namespace std;
int main()
{
pair<char, int> c1(L'x', 3);
printf("[%c, %d", c1.first, c1.second);
c1 = make_pair(L'y', 4);
printf("[{%c}, {%d}]", c1.first, c1.second);
return (0);
}
函数详解:
/** @file stl_pair.h
* This is an internal header file, included by other library headers.
* You should not attempt to use it directly.
*/
#ifndef _PAIR_H
#define _PAIR_H 1
namespace std
{
/// pair holds two objects of arbitrary type.
template<class _T1, class _T2>
struct pair
{
typedef _T1 first_type; ///< @c first_type is the first bound type
typedef _T2 second_type; ///< @c second_type is the second bound type
_T1 first; ///< @c first is a copy of the first object
_T2 second; ///< @c second is a copy of the second object
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 265. std::pair::pair() effects overly restrictive
/** The default constructor creates @c first and @c second using their
* respective default constructors. */
pair()
: first(), second() { }
/** Two objects may be passed to a @c pair constructor to be copied. */
pair(const _T1& __a, const _T2& __b)
: first(__a), second(__b) { }
/** There is also a templated copy ctor for the @c pair class itself. */
template<class _U1, class _U2>
pair(const pair<_U1, _U2>& __p)
: first(__p.first), second(__p.second) { }
};
/// Two pairs of the same type are equal iff their members are equal.
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; }
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); }
/// Uses @c operator== to find the result.
template<class _T1, class _T2>
inline bool
operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
{ return !(__x == __y); }
/// Uses @c operator< to find the result.
template<class _T1, class _T2>
inline bool
operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
{ return __y < __x; }
/// Uses @c operator< to find the result.
template<class _T1, class _T2>
inline bool
operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
{ return !(__y < __x); }
/// Uses @c operator< to find the result.
template<class _T1, class _T2>
inline bool
operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
{ return !(__x < __y); }
/**
* @brief A convenience wrapper for creating a pair from two objects.
* @param x The first object.
* @param y The second object.
* @return A newly-constructed pair<> object of the appropriate type.
*
* The standard requires that the objects be passed by reference-to-const,
* but LWG issue #181 says they should be passed by const value. We follow
* the LWG by default.
*/
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 181. make_pair() unintended behavior
template<class _T1, class _T2>
inline pair<_T1, _T2>
make_pair(_T1 __x, _T2 __y) { return pair<_T1, _T2>(__x, __y); }
} // namespace std
#endif /* _PAIR_H */
有关的例子
#include <iostream>
#include <string>
#include <map>
#include <algorithm> //包含了很多头文件的头文件
typedef int KeyType; //换名称,易懂。
typedef std::pair<const KeyType,std::string> Pair;
//前一个为关键字,后一个为关键字的值
typedef std::multimap<KeyType,std::string> MapCode; //将复杂的名字换为简单的名字
int main(void)
{
using namespace std;
MapCode codes;
//类成员函数插入函数 insert
codes.insert(Pair(415,"San Francisco"));
codes.insert(Pair(510,"Oakland"));
codes.insert(Pair(718,"Brooklyn"));
codes.insert(Pair(718,"Staten Island"));
codes.insert(Pair(510,"San Rafael"));
codes.insert(Pair(415,"Berkeley"));
//用来给没个关键字的值计数
cout << "Number of cities with area code 415: "
<< codes.count(415) << endl;
cout << "Number of cities with area code 510: "
<< codes.count(510) << endl;
cout << "Number of cities with area code 718: "
<< codes.count(718) << endl;
cout << "Area Code City:/n";
MapCode::iterator it; //迭代器
for(it=codes.begin();it!=codes.end();it++)
cout << " " << (*it).first << " " << (*it).second << endl;
pair<MapCode::iterator,MapCode::iterator> range //此处记录迭代器
=codes.equal_range(718);
cout << "Cities with area code 718: " << endl;
//for(it=range.first;it!=range.second;++it)
it=range.first;
cout <<" " << (*it).first << " " << (*it).second << endl;
++it;
cout <<" " << (*it).first << " " << (*it).second << endl;
cin.get();
return 0;
}