Re: for_each algorithm and ostream
On Nov 15, 8:24 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
I want to write data stored in a container into standard
output or an output file using for_each algorithm. To acheive
this, the following program creates a function object of type
'Print'. The argument to the 'Print' class ctor is an ostream
object. This function object is passed as the third argument
to 'for_each' algorithm. The overloaded function call operator
in the 'Print' class, writes the data passed to it into the
ostream member data of the function object.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
class Print
{
public:
explicit Print(ostream& arg);
template <typename T> bool operator()(const T& arg);
private:
ostream& os;
};
Print::Print(ostream& arg) : os(arg)
{
}
template <typename T> inline bool Print::operator()(const T& arg)
{
os << arg << endl;
return true;
}
int main()
{
typedef vector<int> Container;
Container c;
for (int i = 0; i != 10; ++i)
c.push_back(i);
for_each(c.begin(), c.end(), Print(cout));
ofstream ofs("Descending_order");
if (!ofs)
{
cout << "Could not create output file" << endl;
return EXIT_FAILURE;
}
for_each(c.rbegin(), c.rend(), Print(ofs));
return EXIT_SUCCESS;
}
This program works as expected. Is this approach of passing a
function object argument to for_each algorithm for printing
the container data, correct ?
Yes, but...
Kindly reply me if there is a better/cleaner solution and
please provide the same.
You're actually copying, so you should be using std::copy, with
a specialized output iterator. In this case, the specialized
output iterator already exists: std::ostream_iterator.
Otherwise, I'd say that it's more "idiomatic" in some sense to
define a new output iterator, and use it, but that has to be
weighed against the fact that writing an iterator is
considerably more complicated than writing a simple functional
object. (It shouldn't be, of course, but the STL idiom tends to
make things more difficult than they should be.)
--
James Kanze