Is there a more straight-forward way to do this?
for_each(v_Numbers.begin(), v_Numbers.end(), bind1st(operator<<, cout));
Without an expli
yup, using lambda expression (C++ 11) we can inline printing of each element of a STL container to cout.
#include // cout
#include // vector
#include // for_each
#include // istream_iterator
using namespace std;
int main()
{
std::vector v(10,2);
std::for_each(v.begin(), v.end(), [](int i)->void {std::cout << i <
For reading "n" values from cin to vector,
int main()
{
std::vector v;
int elementsToRead;
cin>>elementsToRead; // Number of elements to copy
// Reading from istream
std::istream_iterator ii2(std::cin);
std::copy_n(ii2, elementsToRead, std::back_inserter(v));
// printing updated vector
std::for_each(v.begin(), v.end(), [](int i)->void {cout << i <
(or) by using Lambda expression
std::for_each(std::istream_iterator(cin),std::istream_iterator(),[&v](int i)->void { v.push_back(i);});
To know more about Lambda expression @ What is a lambda expression in C++11?