This does not work. I’m trying to learn how to use std::copy but I can’t find any working code.
I ran this in gcc 4.6.1. And it does not do anything when I hit control D. If I hit Control C…it prints out the new lines only.
Found code here:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<int> userInput;
// Read until end of input.
// Hit control D
std::copy(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter(userInput)
);
// Print in Normal order
std::copy(userInput.begin(),
userInput.end(),
std::ostream_iterator<int>(std::cout,",")
);
std::cout << "\n";
// Print in reverse order:
std::copy(userInput.rbegin(),
userInput.rend(),
std::ostream_iterator<int>(std::cout,",")
);
std::cout << "\n";
}
Not sure how you’re running it or what you’re entering but that seems to run fine for me:
This is with gcc 4.3.4 under Cygwin. One thing to watch out for is that (in my environment at least),
CTRL-Dhas to be entered on a new line. Entering:didn’t work for me (the
CTRL-Dwas ignored) but:did.
You can bypass those issues with
CTRL-Dby doing something like:so that end of file does not depend on your terminal characteristics. This is especially important since it may be that MinGW (being a Windows application rather than running under CygWin’s emulation layer) requires the Windows end-of-file character,
CTRL-Z.Running this command acts as expected: