C++函子到输出迭代器适配器

发布于 2024-08-20 18:31:33 字数 1635 浏览 6 评论 0原文

给定一个适合与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文