在C ++ 11 foreach循环中获取索引

时间:2014-07-22 07:59:18

标签: c++ c++11 foreach

是否有方便的方法在C ++ 11 foreach循环中获取当前容器条目的索引,如python中的enumerate

for idx, obj in enumerate(container):
    pass

我可以想象一个迭代器也可以返回索引或类似的东西。

当然我可以有一个计数器,但通常迭代器不会保证它们在容器上迭代的顺序。

5 个答案:

答案 0 :(得分:16)

可以在此处找到所要求的功能的良好实现:

https://github.com/ignatz/pythonic

背后的想法是,您使用自定义迭代器构建一个包装器结构,以进行计数。下面是一个非常小的示例性实现来说明这个想法:

#include <iostream>
#include <vector>
#include <tuple>

// Wrapper class
template <typename T>
class enumerate_impl
{
public:
    // The return value of the operator* of the iterator, this
    // is what you will get inside of the for loop
    struct item
    {
        size_t index;
        typename T::value_type & item;
    };
    typedef item value_type;

    // Custom iterator with minimal interface
    struct iterator
    {
        iterator(typename T::iterator _it, size_t counter=0) :
            it(_it), counter(counter)
        {}

        iterator operator++()
        {
            return iterator(++it, ++counter);
        }

        bool operator!=(iterator other)
        {
            return it != other.it;
        }

        typename T::iterator::value_type item()
        {
            return *it;
        }

        value_type operator*()
        {
            return value_type{counter, *it};
        }

        size_t index()
        {
            return counter;
        }

    private:
        typename T::iterator it;
        size_t counter;
    };

    enumerate_impl(T & t) : container(t) {}

    iterator begin()
    {
        return iterator(container.begin());
    }

    iterator end()
    {
        return iterator(container.end());
    }

private:
    T & container;
};

// A templated free function allows you to create the wrapper class
// conveniently 
template <typename T>
enumerate_impl<T> enumerate(T & t)
{
    return enumerate_impl<T>(t);
}



int main()
{
    std::vector<int> data = {523, 1, 3};
    for (auto x : enumerate(data))
    {
        std::cout << x.index << ": " << x.item << std::endl;
    }
}

答案 1 :(得分:7)

如下简单的解决方案:

int counter=0;
for (auto &val: container)
{
    makeStuff(val, counter);

    counter++;
}

你可以多做一点&#34;困难&#34;通过添加范围在计数器后面添加代码:

int counter=0;
for (auto &val: container)
{{
    makeStuff(val, counter); 
}counter++;}

正如@ graham.reeds指出的那样,正常的for循环也是一种解决方案,可能同样快:

int counter=0;
for (auto it=container.begin(); it!=container.end(); ++it, ++counter)
{
    makeStuff(val, counter);
}

最后,使用算法的替代方法:

int counter = 0;
std::for_each(container.begin(), container.end(), [&counter](int &val){ 
    makeStuff(val, counter++);
});
  

注意:范围循环和正常循环之间的顺序由标准6.5.4保证。这意味着计数器能够与容器中的位置保持一致。

答案 2 :(得分:3)

C ++ 17和结构化绑定使此操作看起来不错-肯定比带有局部[i = 0](Element&) mutable的丑陋可变lambda更好,或者我在承认可能不是所有内容都应该塞入{{1} } -以及其他要求计数器的范围不在for_each()循环之外的解决方案。

for

如果您经常使用此模式,则可以使它通用:

for (auto [it, end, i] = std::tuple{container.cbegin(), container.cend(), 0};
     it != end; ++it, ++i)
{
      // something that needs both `it` and `i`ndex
}

C ++标准提案P2164建议添加template <typename Container> auto its_and_idx(Container&& container) { using std::begin, std::end; return std::tuple{begin(container), end(container), 0}; } // ... for (auto [it, end, i] = its_and_idx(foo); it != end; ++it, ++i) { // something } ,该提案将提供一个范围视图,该范围为迭代引用的用户提供了元素引用和元素索引。

我们提出一个视图views::enumerate,其值类型为enumerate,其中2个成员structindex分别表示元素在适应范围内的位置和值。

[。 。 。]

此功能以某种形式存在于Python,Rust,Go(支持该语言)和许多C ++库中:valueranges-v3folly({{1} }。

此功能的存在或缺乏是反复出现的堆栈溢出问题的主题。

嘿,看!我们很有名。

答案 3 :(得分:2)

如果您有权使用Boost,则可以像这样使用范围适配器:

using namespace boost::adaptors;

for (auto const& elem : container | indexed(0))
{
    std::cout << elem.index() << " - " << elem.value() << '\n';
}

Source(还有其他示例)

答案 4 :(得分:0)

如果您需要索引,那么传统的索引效果非常好。

for (int idx=0; idx<num; ++idx)
{
// do stuff
}
相关问题