// make_pair example #include <utility> // std::pair #include <iostream> // std::cout int main () { std::pair <int,int> foo; std::pair <int,int> bar; foo = std::make_pair (10,20); bar = std::make_pair (10.5,‘A‘); // ok: implicit conversion from pair<double,char> std::cout << "foo: " << foo.first << ", " << foo.second << ‘\n‘; std::cout << "bar: " << bar.first << ", " << bar.second << ‘\n‘; return 0; }
make_pair
pair.first
pair.second
----------
Sorting a vector of pairs:
sort(changes.begin(), changes.end(), pairCompare);
在pairCompare函数中定义比较方法:
static bool pairCompare(const pair<int, int> pa, const pair<int, int> pb){ return pa.first<pb.first; }
before sorting
2 10
9 -10
3 15
7 -15
5 12
12 -12
15 10
20 -10
19 8
24 -8
after sorting
2 10
3 15
5 12
7 -15
9 -10
12 -12
15 10
19 8
20 -10
24 -8
原文:http://www.cnblogs.com/XingyingLiu/p/5107857.html