std::adjacent_find

 原型:

#include <algorithm>
 forward_iterator adjacent_find( forward_iterator start, forward_iterator end );
 forward_iterator adjacent_find( forward_iterator start, forward_iterator end, BinPred pr );

函数adjacent_find()搜索start和end之间的相邻的两个相同元素. 如果二元谓词pr没有被指定, 则使用它检查两个元素是否相等. 如果被指定,则使用这个pr做operator来对start和end两个iterator指向的元素来做运算。返回值是一个迭代器, 它指向被发现的元素对的第一个元素. 如果没有匹配的元素, 则返回迭代器end.

看下面的例子:

#include <vector>
#include <iostream>
#include <algorithm>
#include <assert.h>

using namespace std;

class TwiceOver
{
public:
	bool operator () (int val1, int val2)
	{
		return val1 == (val2 / 2) ? true: false;
	}
};

int main(int argc, char* argv[] )
{
	int ia[] = { 1, 2, 4, 4, 8 };
	std::vector<int> vec( ia, ia+5);
	int *piter = NULL;
	int *piter2 = NULL;
	vector<int>::iterator iter;
	piter = adjacent_find(ia, ia + 5);
	assert(*piter == ia[2]);

	cout << "The two equal ints' value is:" << *piter << endl;
	iter = adjacent_find(vec.begin(), vec.end(), TwiceOver() );
	//piter2 = adjacent_find(ia,ia+4, TwiceOver() );
	//assert( *piter2 == ia[2] );
	assert( *iter = vec[1] );
	cout << "OK: adjacent_find() succeeded!" << endl;
	cout << "The values of the first occurence of TwiceOver is: "<< *iter << ". " << endl;

	system("pause");
	return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL(标准模板库)提供了许多有用的算法函数,包括以下内容: 1. 非修改性序列操作: `std::all_of`, `std::any_of`, `std::none_of`, `std::for_each`, `std::count`, `std::count_if`, `std::mismatch`, `std::find`, `std::find_if`, `std::find_if_not`, `std::adjacent_find`, `std::search`, `std::search_n`, `std::equal`, `std::is_permutation`, `std::lexicographical_compare`. 2. 修改序列操作:`std::copy`, `std::copy_if`, `std::copy_n`, `std::copy_backward`, `std::move`, `std::move_backward`, `std::fill`, `std::fill_n`, `std::transform`, `std::generate`, `std::generate_n`, `std::replace`, `std::replace_if`, `std::replace_copy`, `std::replace_copy_if`, `std::swap`, `std::swap_ranges`, `std::iter_swap`, `std::reverse`, `std::reverse_copy`, `std::rotate`, `std::rotate_copy`, `std::unique`, `std::unique_copy`. 3. 排序和相关操作:`std::sort`, `std::stable_sort`, `std::partial_sort`, `std::partial_sort_copy`, `std::nth_element`. 4. 二分法操作:`std::lower_bound`, `std::upper_bound`, `std::binary_search`, `std::equal_range`. 5. 堆操作:`std::make_heap`, `std::push_heap`, `std::pop_heap`, `std::sort_heap`. 6. 集合操作:`std::merge`, `std::inplace_merge`, `std::includes`, `std::set_union`, `std::set_intersection`, `std::set_difference`, `std::set_symmetric_difference`. 7. 其他操作:`std::accumulate`, `std::iota`, `std::max`, `std::max_element`, `std::min`, `std::min_element`, `std::next_permutation`, `std::prev_permutation`. 这里只是列举了一些常见的算法函数,还有很多其他函数没有列举出来。这些函数可以在 `<algorithm>` 头文件中找到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值