Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index in C++11 foreach loop

Tags:

c++

foreach

c++11

Is there a convenient way to get the index of the current container entry in a C++11 foreach loop, like enumerate in python:

for idx, obj in enumerate(container):     pass 

I could imagine an iterator that can also return the index or similar.

Of course I could have a counter, but often iterators don't give guarantees of the order they iterate over a container.

like image 229
hildensia Avatar asked Jul 22 '14 07:07

hildensia


People also ask

How do you find the index value of A for each loop?

Luckily, there are several ways to get an index variable with foreach : Declare an integer variable before the loop, and then increase that one inside the loop with each loop cycle. Create a tuple that returns both the element's value and its index. Or swap the foreach loop with the for loop.

How do you get the index of the current iteration of a foreach loop?

C# Program to Get the index of the Current Iteration of a foreach Loop Using Select() Method. The method Select() is a LINQ method. LINQ is a part of C# that is used to access different databases and data sources. The Select() method selects the value and index of the iteration of a foreach loop.

How do you find the index of a vector?

Access an element in vector using vector::at() reference at(size_type n); reference at(size_type n); It returns the reference of element at index n in vector. If index n is out of range i.e. greater then size of vector then it will throw out_of_range exception.


1 Answers

A good implementation of the feature you are requested can be found here:

https://github.com/ignatz/pythonic

The idea behind is, that you build a wrapper struct with a custom iterator that does the counting. Below is a very minimal exemplary implementation to illustrate the idea:

// Distributed under the terms of the GPLv2 or newer  #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;     } } 
like image 191
CK1 Avatar answered Oct 18 '22 21:10

CK1