Q:

Use stream iterators, sort, and copy to read a sequence of integers from the standard input, sort them, and then write them back to the standard output

0

Use stream iterators, sort, and copy to read a sequence of integers from the standard input, sort them, and then write them back to the standard output.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

int main() {
  std::istream_iterator<int> i_iter(std::cin), i_eof;
  //std::vector<int> vi;
  //while (i_iter != i_eof)
  //  vi.push_back(*i_iter++);
  std::vector<int> vi(i_iter, i_eof);
  std::sort(vi.begin(), vi.end());
  std::ostream_iterator<int> o_iter(std::cout, " ");
  std::copy(vi.begin(), vi.end(), o_iter);

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now