Home >Backend Development >C++ >How can I get the index of an element while iterating through a vector using a range-based for loop in C ?

How can I get the index of an element while iterating through a vector using a range-based for loop in C ?

Susan Sarandon
Susan SarandonOriginal
2024-12-05 03:18:11931browse

How can I get the index of an element while iterating through a vector using a range-based for loop in C  ?

Index Extraction using Range-Based For Loops

In C programming, a range-based for loop offers a concise way to iterate over elements of a container. However, sometimes it becomes necessary to determine the index of the current element during the iteration without relying on an additional iterator.

Problem:

Given a vector of values and a range-based for loop as shown below:

vector<int> list;
for(auto& elem:list) {
    int i = elem;
}

Is there a way to find the position of elem within the vector without maintaining a separate iterator?

Solution:

Yes, it is possible to extract the index while iterating through a range-based for loop. This technique involves a combination of templated structures and iterators:

template <typename T>
struct iterator_extractor { typedef typename T::iterator type; };

template <typename T>
struct iterator_extractor<T const&> { typedef typename T::const_iterator type; };

template <typename T>
class Indexer {
public:
    class iterator {
        typedef typename iterator_extractor<T>::type inner_iterator;
        typedef typename std::iterator_traits<inner_iterator>::reference inner_reference;
    public:
        typedef std::pair<size_t, inner_reference&> reference;

        iterator(inner_iterator it): _pos(0), _it(it) {}

        reference operator*() const { return reference(_pos, *_it); }

        iterator& operator++() { ++_pos; ++_it; return *this; }
        iterator operator++(int) { iterator tmp(*this); ++*this; return tmp; }

        bool operator==(iterator const& it) const { return _it == it._it; }
        bool operator!=(iterator const& it) const { return !(*this == it); }

    private:
        size_t _pos;
        inner_iterator _it;
    };

    Indexer(T& t): _container(t) {}

    iterator begin() const { return iterator(_container.begin()); }
    iterator end() const { return iterator(_container.end()); }

private:
    T& _container;
}; // class Indexer

template <typename T>
Indexer<T> index(T& t) { return Indexer<T>(t); }

This code snippet provides the ability to iterate over a container while capturing the index and the value simultaneously. The Indexer class wraps the container and provides a custom iterator that yields pairs of indices and element references.

Example Usage:

To use the Indexer class in a range-based for loop, you can do the following:

#include <iostream>
#include <limits>
#include <vector>

int main() {
    std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9};

    for (auto p: index(v)) {
        std::cout << p.first << ": " << p.second << "\n";
    }
}

In this example, the loop will print each element of the vector along with its index:

0: 1
1: 2
2: 3
3: 4
4: 5
5: 6
6: 7
7: 8
8: 9

The above is the detailed content of How can I get the index of an element while iterating through a vector using a range-based for loop in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn