vendredi 1 avril 2016

std::istream_iterator stops advancing

Why does the following program output

1 2 3 4 4 4

and not

1 2 3 4 5 6

for each of the values provided?

#include <iostream>
#include <iterator>
#include <vector>
#include <string>
#include <sstream>

int main()
{

    std::vector<int> numbers;
    std::stringstream ss;
    ss << " 1 2";
    std::istream_iterator<int> start{ss},end;
    ss << " 3 4";
    numbers.push_back(*start++);
    numbers.push_back(*start++);
    numbers.push_back(*start++);
    ss << " 5 6";
    numbers.push_back(*start++);
    numbers.push_back(*start++);
    numbers.push_back(*start++);

    std::cout << "numbers read in:\n";
    for (auto number : numbers) {
        std::cout << number << " ";
    }
    std::cout << "\n";
}

Aucun commentaire:

Enregistrer un commentaire