Why does the following code snippet behave weirdly when it1 is constructed each time inside the while loop? In VS 2015, the loop does not terminate. Using gcc 4.9 it prints empty words after the first one. If I do not construct it1 inside the while loop, the loops works as expected? Does iterators do some kind of lazy evaluation?
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
int main () {
std::string S("Hello world, the, quick, brown, fox, jumps, over");
std::vector<char> V(S.begin(), S.end());
std::vector<char>::const_iterator it = V.cbegin();
std::vector<char>::const_iterator it1 = std::find(it, V.cend(), ',');
while (it1 != V.cend()) {
std::cout <<"the string is: " << std::string(it, it1) << std::endl;
it = ++it1;
std::vector<char>::const_iterator it1 = std::find(it, V.cend(), ',');
//it1 = std::find(it, V.cend(), ',');
}
}
Aucun commentaire:
Enregistrer un commentaire