算法之旅,直奔(algorithm)之二 adjacent_find

2014-11-24 03:26:02 · 作者: · 浏览: 0

adjacent_find(vs2010版本)

引言 虽然这是集成的函数,不过却为我们大家使用提供了很大的方便。我的感受是集成的东西不只是用来开发的,也可以用来搞实验测试和学习。这是我总结的 里的第二个函数 adjacent_find.
作用 adjacent_find 的作用是在容器里找到相邻元素符合自定义条件的第一个元素对,并返回这个元素对的第一个向量。
实验

在数据集合{20,20,5,30,30,30,20,10,10,20}里找到所有的相邻元素相同的元素对,并返回元素对首元素的向量。


\



代码

// adjacent_find example
#include 
   
         // std::cout
#include 
    
      // std::adjacent_find #include 
     
       // std::vector bool Condition (int i, int j) { return ( i == j ); } int main () { int myints[ 10 ] = {20,20,5,30,30,30,20,10,10,20}; std::vector
      
        myvector ( myints , myints+9 ); std::vector
       
        ::iterator it; // using default comparison: it = std::adjacent_find ( myvector.begin(), myvector.end(), Condition ); while( it != myvector.end() ) { std::cout << "the second pair of repeated elements are: " << *it << " " << int( it - myvector.begin() ) << '\n'; it = std::adjacent_find ( ++it, myvector.end(), Condition ); } system("pause"); return 0; }