jeudi 29 novembre 2018

Python's enumerate for C++

In Python there is enumerate which takes a sequence/iterator and yields pairs of an integer index and the value itself. In C++ I occasionally find myself writing

for (size_t i = 0; i != vector.size(); ++i) {
    auto const &elem = vector[i];
    // ...

Similar to Python I would like to write

for (auto const &it : enumerate(vector)) {
    // it.first is the index (size_t)
    // it.second is the element (T const&)

Does such an enumerate exist in either the STL or a common library like Boost?

Aucun commentaire:

Enregistrer un commentaire