算法之旅,直奔<algorithm>之二 adjacent_find

35 篇文章 1 订阅
29 篇文章 0 订阅

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值