for_each给出两个(或n个)相邻的元素 [英] for_each that gives two (or n) adjacent elements

查看:245
本文介绍了for_each给出两个(或n个)相邻的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 for_each 的标准实现可以调用元素和下一个元素?



例如,取值范围 {0,1,2,3,4,5} ,我想调用一个函数 f 与每个元素及其后继: {f(0,1),f(1,2),f(2,3),f(3,4),f 4,5)}
请注意最后一个元素是如何被遗漏的,因为它没有后继元素。

这也不错如果有这样的推广到n元素本身的传递。



到现在为止,我一直用迭代器的手写循环来解决这个问题。
然而,我想要更多的沿着基于C ++ 11范围的行或 std :: for_each 来避免锅炉板代码。例如

  //今天:手写循环
for(b

Range :: iterator current = range.begin(); current!= range.end(); ++ current)
f(* current,* std :: next(current));

//近期包装版本
for_each_pair(range.begin(),range.end(),f);

//稍微远一点的未来:泛化版本
for_each_tuple< n>(range.begin(),range.end(),f);



附加问题



功能可以改善。对我来说, for_each_pair / 元组听起来像范围的大小为n的所有子集应该被返回(这本身就是另一个问题我想解决)。因此,我希望提供更好的名称建议,例如:

  for_each_adjacent< n> 


解决方案

您实际上可能会滥用 std :: unique 或者 std :: adjacent_find 为此:谓词在迭代器范围内的每个连续对被调用,并且只要谓词总是返回false,它不会修改任何内容或提前返回。



忽略这个特殊的黑客行为,我会把它作为一个迭代器适配器,而不是一个算法。也就是说,我会实现一个 consecutive_tuple_iterator< N> ,它会返回N个连续项目的所有元组。那么你可以用它来做 count_if 包含,而不仅仅是 for_each 。 (尽管大多数修改算法都不适用。)

Is there a standard implementation of a for_each that does call with the element and the next one in the range?

For example take the range {0, 1, 2, 3, 4, 5}, I would like to call a function f with each element and its successor: {f(0, 1), f(1, 2), f(2, 3), f(3, 4), f(4, 5)} Note how the last element is left out because it has no successor.

It would also be nice if there was a generalization of this to n successors that get passed with the element itself.

Up to now I have always solved this in terms of a handwritten loop with iterators. However, I would like to go much more along the lines of C++11 range based for or std::for_each to avoid boiler plate code.

Examples

// today: handwritten loop
for(Range::iterator current = range.begin(); current != range.end(); ++current) 
   f(*current, *std::next(current));

// near future: wrapped version
for_each_pair(range.begin(), range.end(), f);

// slightly further future: generalized version
for_each_tuple<n>(range.begin(), range.end(), f);

Additional Question

The name of the function could be improved. To me for_each_pair/tuple sounds like all subsets of size n of the range should be returned (which is in itself another problem I would like to solve). So I'd like some suggestions on better names like:

for_each_adjacent<n>

解决方案

You could actually abuse std::unique or std::adjacent_find for this: the predicate is called with each consecutive pair in the iterator range, and as long as the predicate always returns false, it won't modify anything or return early.

Disregarding that particular hack, I would implement this as an iterator adapter, not an algorithm. That is, I'd implement a consecutive_tuple_iterator<N> which would return all tuples of N consecutive items. Then you could use it for things like count_if and includes, not just for_each. (It wouldn't be appropriate for most modifying algorithms, though.)

这篇关于for_each给出两个(或n个)相邻的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆