为成对容器的第一个元素提供迭代器

发布于 2024-07-09 22:34:57 字数 251 浏览 4 评论 0原文

我有一个装满成对的容器。 我想使用 STL 通用算法对其进行迭代(在我的例子中它将是inner_product,但将其视为通用问题)。 我使用的算法首先和最后需要迭代器。 我可以在第一个和最后一个提供特殊的迭代器,该迭代器不会在对上迭代,而是在每对的第一个元素上迭代吗?

我知道我可以手动完成,提供一个手工制作的函数对象,它将成为标准容器迭代器的包装器,将其遵循该对本身的第一个成员,但我认为还有一个聪明的方法-liner 帮我做这件事。 会是什么?

I have a container filled with pairs. I want to iterate in it using the STL generic algorithms (in my case it would be inner_product, but consider it as a generic problem).
The algorithm I am using expects iterators first and last. Can I provide special iterators first and last that will iterate not on the pairs but on the first element of each pair?

I know i can do it manually, providing a hand-made function object that will be a wrapper around the standard container iterator, deferencing it to the first member of the pair intend of the pair itself,but I think there is also a clever one-liner to do it for me. What would it be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

少女的英雄梦 2024-07-16 22:34:57

我环顾四周,发现了 boost::transform_iterator。 我想出了这段代码。 令人惊讶的是它的工作效果如何:

#include <map>
#include <algorithm>
#include <iostream>
#include <string>
#include <iterator>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

int main() {
    typedef std::map<std::string, int>::value_type value_type;
    std::map<std::string, int> a;
    a["one"] = 1;
    a["two"] = 2;

    // returns the second element 
    boost::function<int(value_type&)> f = boost::bind(&value_type::second, _1);
    std::copy(boost::make_transform_iterator(a.begin(), f), 
              boost::make_transform_iterator(a.end(), f),
              std::ostream_iterator<int>(std::cout, " "));

}

它将 "1 2 " 打印到标准输出。

I've looked around and found boost::transform_iterator. I've come up with this code. Surprising how well it works:

#include <map>
#include <algorithm>
#include <iostream>
#include <string>
#include <iterator>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

int main() {
    typedef std::map<std::string, int>::value_type value_type;
    std::map<std::string, int> a;
    a["one"] = 1;
    a["two"] = 2;

    // returns the second element 
    boost::function<int(value_type&)> f = boost::bind(&value_type::second, _1);
    std::copy(boost::make_transform_iterator(a.begin(), f), 
              boost::make_transform_iterator(a.end(), f),
              std::ostream_iterator<int>(std::cout, " "));

}

It's printing "1 2 " to the standard output.

锦欢 2024-07-16 22:34:57

没有一种聪明的一劳永逸的解决方案。 您最好的希望是编写一个包装迭代器。 这实际上是一个非常规范的解决方案。 您可以检查 Boost 是否已经有您需要的东西。 如果没有,请尝试编写一个可重用于其他问题的通用包装器。

STL 包含这样一个称为reverse_iterator 的迭代器包装器。 这个名字暗示了它的用途。

There is no clever one-liner solution. Your best hope is to write a wrapper iterator. This is actually a pretty canonical solution. You might check whether Boost already has what you need. If not, try to write a generic wrapper that can be reused for other problems.

The STL contains such an iterator wrapper called reverse_iterator. The name implies its use.

悲歌长辞 2024-07-16 22:34:57

最终,我认为你的想法是可行的方法。 您可以使用 Boost 来帮助您做到这一点。 首先,您需要一个函数来获取您的对并返回第一个元素。 我认为你可以使用 Lambda 编写这样的函数库,但为了可读性,我想我只需编写一个简单的函数即可实现此目的。 然后将该函数与原始迭代器一起传递以构造 < code>transform_iterator 用于序列的开始和结束。

Ultimately, I think your idea is the way to go. You can use Boost to help you do it. To start, you'd need a function that takes your pair and returns the first element. I think you could write such a function in-line using the Lambda library, but for readability's sake, I think I'd just write a simple function that does that instead. Then pass that function with your original iterators to construct a transform_iterator for your sequence's begin and end.

薄荷梦 2024-07-16 22:34:57

您可以自己对例如 std::vector::const_iterator 进行子类化,重新实现operator* 和operator->; 返回该对中的第一个。 您还需要创建自己的 begin() 和 end() 函数来返回自定义迭代器。

您还可以创建二进制函数类并将它们传递给inner_product。

You can subclass e.g. std::vector::const_iterator yourself, reimplementing operator* and operator-> to return the first of the pair. You'd also need to create your own begin() and end() functions to return your custom iterator.

You can also create to binary function classes and pass those to inner_product.

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