mardi 25 juillet 2017

How to iterate over std::array and extract each time 2 elements in a certain distance?

Is there a cleaner way to write something like the following:

#include <iostream>
#include <iterator>
#include <array>

int main() 
{
    std::array<int, 6> arr{ {0, 1, 2, 3, 4, 5} };

    for (unsigned int i = 0; i < arr.size()/2; i++)
        std::cout << arr[i] << " " << arr[i+3] << std::endl;

}

The output is:

0 3
1 4
2 5

I want to iterate over an std::array and take each time 2 elements with a distance of 3. The thing is that I feel there should be a cleaner way to do it and also using unsigned int looks a bit weird to me.

Aucun commentaire:

Enregistrer un commentaire