#include "algostuff.hpp"
using namespace std;
bool doubled(int elem1,int elem2)
{
 return elem1*2==elem2;
}
int main()
{
 vector<int> coll;
 coll.push_back(1);
 coll.push_back(3);
 coll.push_back(2);
 coll.push_back(4);
 coll.push_back(5);
 coll.push_back(5);
 coll.push_back(0);

 PRINT_ELEMENTS(coll,"coll: ");
 vector<int>::iterator pos;
 pos=adjacent_find(coll.begin(),coll.end());
 if(pos!=coll.end())
 {
  cout<<"first two elements with equal value have position "
   <<distance(coll.begin(),pos)+1
   <<endl;
 }

 pos=adjacent_find(coll.begin(),coll.end(),doubled);
 if(pos!=coll.end())
 {
  cout<<"first two elements with second value twice the first have pos. "
   <<distance(coll.begin(),pos)+1
   <<endl;
 }
}

////////////////////////////////////////////////////////////////////////////////////////////////

output:

coll: 1 3 2 4 5 5 0
first two elements with equal value have position 5
first two elements with second value twice the first have pos. 3
Press any key to continue 

posted on 2010-04-17 20:04  蓝牙  阅读(174)  评论(0编辑  收藏  举报