V
V.Subramanian, India
In the following program,
I have kept the data members ONLY for learning purpose.
#include <cstdlib>
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
class Person {
public:
explicit Person(const string& arg);
string name;
};
inline Person:erson(const string& arg) : name(arg)
{
}
class Club {
public:
list<Person*> officers;
};
class Print_name {
public:
Print_name(ostream& os);
void operator()(const Person* const & arg) const;
private:
ostream& out;
};
inline Print_name:rint_name(ostream& os) : out(os)
{
}
inline void Print_name:perator()(
const Person* const & arg) const
{
out << arg->name << endl;
}
int main()
{
Club c;
c.officers.push_back(new Person(string("one")));
c.officers.push_back(new Person(string("two")));
c.officers.push_back(new Person(string("three")));
for_each(c.officers.begin(),
c.officers.end(),
Print_name(cout));
return EXIT_SUCCESS;
}
The above program uses 'std::for_each()'
algorithm. The third argument to this
algorithm is a function object 'Print_name'.
The argument to the function object is the
output stream into which we want to write
the contents.
Since the container elements are pointers,
I am unable to use the 'std::copy()' algorithm
with ostream_iterator because the value
which will be passed to ostream_iterator
is a pointer.
something like:
copy(c.officers.begin(),
c.officers.end(),
ostream_iterator<Person*>(cout, "\n"));
This wil simply print the memory addresses
to the standard output. Am I correct ?
Is there a clean solution instead of the
'std::for_each()' algorithm with the
'Print_name' function object
combination which I have used in the
above program ?
Please provide the solution.
Is it possible to use the 'std::copy()' algorithm
for the above scenario ?
Thanks
V.Subramanian
India.
I have kept the data members ONLY for learning purpose.
#include <cstdlib>
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
class Person {
public:
explicit Person(const string& arg);
string name;
};
inline Person:erson(const string& arg) : name(arg)
{
}
class Club {
public:
list<Person*> officers;
};
class Print_name {
public:
Print_name(ostream& os);
void operator()(const Person* const & arg) const;
private:
ostream& out;
};
inline Print_name:rint_name(ostream& os) : out(os)
{
}
inline void Print_name:perator()(
const Person* const & arg) const
{
out << arg->name << endl;
}
int main()
{
Club c;
c.officers.push_back(new Person(string("one")));
c.officers.push_back(new Person(string("two")));
c.officers.push_back(new Person(string("three")));
for_each(c.officers.begin(),
c.officers.end(),
Print_name(cout));
return EXIT_SUCCESS;
}
The above program uses 'std::for_each()'
algorithm. The third argument to this
algorithm is a function object 'Print_name'.
The argument to the function object is the
output stream into which we want to write
the contents.
Since the container elements are pointers,
I am unable to use the 'std::copy()' algorithm
with ostream_iterator because the value
which will be passed to ostream_iterator
is a pointer.
something like:
copy(c.officers.begin(),
c.officers.end(),
ostream_iterator<Person*>(cout, "\n"));
This wil simply print the memory addresses
to the standard output. Am I correct ?
Is there a clean solution instead of the
'std::for_each()' algorithm with the
'Print_name' function object
combination which I have used in the
above program ?
Please provide the solution.
Is it possible to use the 'std::copy()' algorithm
for the above scenario ?
Thanks
V.Subramanian
India.