adjacent_find(vs2010版本)
- 引言
虽然这是集成的函数,不过却为我们大家使用提供了很大的方便。我的感受是集成的东西不只是用来开发的,也可以用来搞实验测试和学习。这是我总结的<algorithm>里的第二个函数 adjacent_find.
- 作用
adjacent_find 的作用是在容器里找到相邻元素符合自定义条件的第一个元素对,并返回这个元素对的第一个向量。
- 实验
在数据集合{20,20,5,30,30,30,20,10,10,20}里找到所有的相邻元素相同的元素对,并返回元素对首元素的向量。
- 代码
// adjacent_find example
#include <iostream> // std::cout
#include <algorithm> // std::adjacent_find
#include <vector> // 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<int> myvector ( myints , myints+9 );
std::vector<int>::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;
}
// adjacent_find example
#include <iostream> // std::cout
#include <algorithm> // std::adjacent_find
#include <vector> // 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<int> myvector ( myints , myints+9 );
std::vector<int>::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;
}