#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
struct Area { Area():x(0), y(0) {} Area(int a, int b):x(a),y(b){} int x,y; bool operator<(const Area &rhs) { return x<rhs.x; } //排序用到 friend ostream& operator<<(ostream &out, const Area &rhs); //输出用到 }; ostream& operator<<(ostream &out, const Area &rhs) { out << "["<< rhs.x <<"," << rhs.y << "]"; return out; } int _tmain(int argc, _TCHAR* argv[]) { vector<Area> vec; vec.push_back(Area(7,2)); vec.push_back(Area(5,6)); vec.push_back(Area(1,8)); copy(vec.begin(), vec.end(), ostream_iterator<Area>(cout, " ")); cout << endl; sort(vec.begin(), vec.end()); copy(vec.begin(), vec.end(), ostream_iterator<Area>(cout, " ")); cout << endl; }
The copy() function copies the elements between start and end to dest.
ostream_iterator<Area>(cout, " "):定义输出流迭代器