#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector<int> test_int;
for(int i=1;i<=3;i++)
{
test_int.push_back(i);
test_int.push_back(i);
test_int.push_back(i);
} //111 222 333
pair<vector<int>::iterator,vector<int>::iterator> bound_by_two;
bound_by_two=equal_range(test_int.begin(),test_int.end(),2);
cout<<"before sort,tail of bound by two is: "<<*bound_by_two.second<<endl;
bool myfunction (int i,int j);
sort(test_int.begin(),test_int.end(),myfunction); //333 222 111
cout<<"after sort,tail of bound by two is: "<<*bound_by_two.second<<endl;
}
bool myfunction (int i,int j) { return (i>j); }
运行结果:
before sort,tail of bound by two is: 3
after sort,tail of bound by two is: 1
在c++ reference 里面关于vector.end()的返回值是这样说明的:
return an iterator to the element past the end of the sequence
试验结果表明:
当你用sort去改变原来的vector元素的顺序时,排序之前所做的.end()操作也将返回重新排序后的相应的iterator值。