C++ Learn to use stream_iterators
Try not to use explicit loops, but prefer the standard algorithms.
One easy way to practice with this is using the stream_iterators:
int main()
{
std::vector<int> data;
// copy a stream of numbers from standard input to
// vector, then back out to the standard output.
std::copy(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::,back_inserter(data)
);
std::copy(std::begin(data), std::end(data),
std::ostream_iterator<int>(std::cout));
}
Header needed for the above code:
#include <vector>
#include <ostream>
#include <algorithm>
#include <iterator>
(placed here to make the code easier to read in one chunk)
Written by LokiAstari
Related protips
2 Responses
You have a typo: extra comma before back_inserter
.
I wonder what is the cost of these iterators? You're creating objects that will be copied and original ones will be destroyed (until C++11 and its move semantics).
Also some justification words would be nice to read.
over 1 year ago
·
I agree with @barmaley-exe; it would be nice to see some justification of why this is worthwhile/important.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Algorithms
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#