[ACCEPTED]-How do I use for_each to output to cout?-cout
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>(cout));
It 7 would be even nicer if you add some suffix:
std::copy(v_Numbers.begin(), v_Numbers.end(),
std::ostream_iterator<int>(cout, "\n"));
This 6 assumes that your container is a vector<int>
, so you 5 will have to replace that part with the 4 appropriate type.
Edit regarding reading input:
Conversely, you 3 can copy from a range of std::istream_iterator
into a vector
using 2 std::back_inserter
:
std::vector<int> v_Numbers;
std::copy(std::istream_iterator<int>(cin), std::istream_iterator<int>(),
std::back_inserter(v_Numbers));
If you want to read n elements only, look 1 at this question.
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));
}
0
yup, using lambda expression (C++ 11) we 5 can inline printing of each element of a 4 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 3 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 2 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 1 @ What is a lambda expression in C++11?
Not always appropriate in corporate code, but 12 for the sake of enumerating options - if 11 you really find other for_each / std::copy 10 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 9 much nicer if you're well-mannered (;-p) enough 8 to only overload your specific instantiation 7 of vector (which requires My_Type be more 6 than a typedef to say int, though it's not 5 hard to create a templated class to create 4 new types wrapping an arbitrary type). Otherwise, if 3 someone else does the same elsewhere in 2 your translation unit, the streaming could 1 become ambiguous.
I know the copy
with the iterator is the optimal 3 solution, but just to answer with for_each
.
You could 2 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 1 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;
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.