haierophanto
haierophanto

Reputation: 51

How to implement auto iterators correctly

I decided to replace

for (auto messageIterator = message.begin(); messageIterator != message.end(); ++messageIterator)

with

for (auto &messageIterator : message)

and it works. Then I decided to apply similar approach to this loop

for (auto alphabetIterator = alphabet.begin(), rotorIterator = rotor.begin(); alphabetIterator != alphabet.end(), rotorIterator != rotor.end(); ++alphabetIterator, ++rotorIterator)

and my code looks like this but it doesn't work.

for (auto &alphabetIterator : alphabet, &rotorIterator : rotor)

How do I fix it?

Upvotes: 0

Views: 74

Answers (1)

Jarod42
Jarod42

Reputation: 217810

With range-v3, you might do:

std::vector<int> vi{1, 2, 3};
std::vector<std::string> vs{"one", "two", "three"};

for (const auto& [i, s] : ranges::view::zip(vi, vs)) {
    std::cout << i << " " << s << std::endl;   
}

Demo

Upvotes: 2

Related Questions