19

Is there a more straight-forward way to do this?

for_each(v_Numbers.begin(), v_Numbers.end(), bind1st(operator<<, cout));

Without an explicit for loop, if possible.

EDIT:

How to do this for std::cin with a std::vector if possible? (How to read n elements only)?

nakiya
  • 14,063
  • 21
  • 79
  • 118

6 Answers6

36

You could achieve this using std::copy into a std::ostream_iterator:

std::vector<int> v_Numbers; // suppose this is the type
// put numbers in
std::copy(v_Numbers.begin(), v_Numbers.end(),
          std::ostream_iterator<int>(std::cout));

It would be even nicer if you add some suffix:

std::copy(v_Numbers.begin(), v_Numbers.end(),
          std::ostream_iterator<int>(std::cout, "\n"));

This assumes that your container is a vector<int>, so you will have to replace that part with the appropriate type.

Edit regarding reading input:

Conversely, you can copy from a range of std::istream_iterator into a vector using std::back_inserter:

std::vector<int> v_Numbers;
std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(),
          std::back_inserter(v_Numbers));

If you want to read n elements only, look at this question.

Chris
  • 8,030
  • 4
  • 37
  • 56
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • 1
    Almost. `ostream_iterator` is a template, you want `std::ostream_iterator(cout)`. – GManNickG Nov 11 '10 at 09:52
  • 2
    No problem; I formatted the answer so there's no scroll-bars, I find it easier to read that way. You can disagree and roll-back if you want. – GManNickG Nov 11 '10 at 09:54
  • Sadly, it does not seem to work : `error: v_Numbers cannot appear in a constant-expression` – nakiya Nov 11 '10 at 09:56
  • @nakiya: An error is only half the information; what's the code? (EDIT: Er, you need to `copy` to output, not `for_each`. Missed that, sorry Space.) – GManNickG Nov 11 '10 at 09:57
  • @nakiya: Indeed, you have to replace `v_Numbers` in the template argument with the type of `v_Numbers` (e.g. `std::vector::value_type`). – Björn Pollex Nov 11 '10 at 09:58
  • I think you meant `std::copy` there. `for_each` expects an `operator()` that accepts a `v_Numbers::value_type` – MSalters Nov 11 '10 at 09:59
10

Another option — Boost.Lambda.

for_each(v.begin(), v.end(), cout << boost::lambda::_1);
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
8

Yep, but you must use std::copy algorithm:

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

int main()
{
    std::vector<int> a;
    // fill a...
    std::copy(a.begin(), a.end(), std::ostream_iterator<int>(std::cout));
}
Simone
  • 11,655
  • 1
  • 30
  • 43
6

yup, using lambda expression (C++ 11) we can inline printing of each element of a STL container to cout.

#include <iostream>   // cout
#include <vector>     // vector
#include <algorithm>  // for_each
#include <iterator>   // istream_iterator
using namespace std;

int main()
{
   std::vector<int> v(10,2);
   std::for_each(v.begin(), v.end(), [](int i)->void {std::cout << i <<endl;});
   return 0;
}

For reading "n" values from cin to vector,

 int main()
 {
   std::vector<int> v;

   int elementsToRead;
   cin>>elementsToRead;  // Number of elements to copy

   // Reading from istream
   std::istream_iterator<int> 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 <<endl;});

   return 0;
}

(or) by using Lambda expression

std::for_each(std::istream_iterator<int>(cin),std::istream_iterator<int>(),[&v](int i)->void { v.push_back(i);});

To know more about Lambda expression @ What is a lambda expression in C++11?

Community
  • 1
  • 1
0

Not always appropriate in corporate code, but for the sake of enumerating options - if you really find other for_each / std::copy etc. solutions too verbose, you could write:

std::ostream& operator(std::ostream& os, const std::vector<My_Type>& v)
{
     // pick one of the other implementations for here...
    std::copy(std::istream_iterator<My_Type>(os), std::istream_iterator<My_Type>(),   
          std::back_inserter(v_Numbers));   
}

It's much nicer if you're well-mannered (;-p) enough to only overload your specific instantiation of vector (which requires My_Type be more than a typedef to say int, though it's not hard to create a templated class to create new types wrapping an arbitrary type). Otherwise, if someone else does the same elsewhere in your translation unit, the streaming could become ambiguous.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

I know the copy with the iterator is the optimal solution, but just to answer with for_each.

You could do:

#include <vector>
#include <algorithm>
#include <locale>

int main() {
  using namespace std;
  locale::global(locale(""));
  wcout::imbue(locale());

  vector<int> vec{1000,2000,3000,4000,5000};
  for_each(vec.begin(), vec.end(), [](auto &x){wcout << x << endl;});
  
  return 0;
}

But, for me, it's REALLY much more readable the simple for ...

#include <vector>
#include <locale>

int main() {
  using namespace std;
  locale::global(locale(""));
  wcout::imbue(locale());

  vector<int> vec{1000,2000,3000,4000,5000};
  for(auto &v: vec) {
    wcout << v << endl;
  }
  
  return 0;
}
Bigous
  • 381
  • 3
  • 7