C++函子到输出迭代器适配器
给定一个适合与 std::for_each 和朋友一起使用的函子:
template <typename T> struct Foo {
void operator()(T const& t) { ... }
};
std::for_each(v.begin(), v.end(), Foo<Bar>());
是否有某种标准方法可以将其转换为适合与 std::copy 和朋友一起使用的输出迭代器? (或相反的适应)类似:
std::copy(v.begin(), v.end(), functor_output_iterator(Foo<Bar>()));
每次将值分配给迭代器时都会调用函子:
adapter(F f) : functor(f) { }
adapter& operator*() { return *this; }
operator=(T const& t) { functor(t); }
operator++() { }
...
或者,或者:
std::for_each(..., some_adapter(std::ostream_iterator(std::cout)));
背景:
我有一个使用输出迭代器公开集合的类:
template <typename It> GetItems(It out) {
MutexGuard guard(mutex);
std::copy(items.begin(), items.end(), out);
}
这允许调用者访问物品,而不强迫他们使用特定的容器类型,也不会弄乱锁定或其他内部细节。
例如,只获取独特的项目:
std::set<whatever> set;
obj->GetItems(std::inserter(set, set.end()));
这比以下更好:
ObjLock lock = obj->GetLock();
for (int i = 0; i < obj->GetItemCount(); ++i) {
Item* item = obj->GetItem(i);
...
现在我还希望能够聚合这些项目,而不是复制它们。 (请参阅此问题)。我通常会做类似的事情:
std::for_each(v.begin(), v.end(), Joiner<Foo>());
现在我可以为相同的数据元素创建两种单独的方法,一种调用 std::copy
,另一种调用 std::for_each
但如果能够使用迭代器或函子来定义一个这样的方法,并且让调用者能够将函子或迭代器传递给它,并根据需要将它们调整为适当的类型,那就太好了。
我现在正在做的是定义聚合器,使其可以用作输出迭代器或函子,但这会导致不必要的复杂性。
Given a functor appropriate for use with std::for_each
and friends:
template <typename T> struct Foo {
void operator()(T const& t) { ... }
};
std::for_each(v.begin(), v.end(), Foo<Bar>());
Is there some standard way to convert this into an output iterator appropriate for use with std::copy
and friends? (or the opposite adaptation) Something like:
std::copy(v.begin(), v.end(), functor_output_iterator(Foo<Bar>()));
Which would call the functor each time a value is assigned to the iterator:
adapter(F f) : functor(f) { }
adapter& operator*() { return *this; }
operator=(T const& t) { functor(t); }
operator++() { }
...
Or, alternatively:
std::for_each(..., some_adapter(std::ostream_iterator(std::cout)));
Background:
I have a class that exposes a collection using an output iterator:
template <typename It> GetItems(It out) {
MutexGuard guard(mutex);
std::copy(items.begin(), items.end(), out);
}
This allows callers to get access to the items without forcing them to use a specific container type and without messing around with locking or other internal details.
e.g., to get only unique items:
std::set<whatever> set;
obj->GetItems(std::inserter(set, set.end()));
This beats the hell out of:
ObjLock lock = obj->GetLock();
for (int i = 0; i < obj->GetItemCount(); ++i) {
Item* item = obj->GetItem(i);
...
Now I also want to be able to aggregate these items, rather than copying them. (See this question). Where I would normally do something like:
std::for_each(v.begin(), v.end(), Joiner<Foo>());
Now I could make two separate methods for the same data elements, one that calls std::copy
and one that calls std::for_each
but it would be nice to be able to define just one such method, using an iterator or a functor, and have callers be able to pass either functors or iterators to it, adapting them as necessary to the appropriate type.
What I'm doing now is defining the aggregator in such a way that it can be used as either an output iterator or a functor, but this leads to unwanted complexity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
boost::function_output_iterator怎么样?
How about boost::function_output_iterator?