Listing 4
// Alternative iterations using search objects
// Alternative 1.
// The search class has a method "end()" that can be used instead of
// an past-the-end iterator, like "end_s". The iteration then looks like:
search<Person> s(db, "Select Name, Age from Persons");
for (;s != s.end(); ++s)
std::cout << s->name_ << " " << s->age_ << std::endl;
// Alternative 2.1
// Iteration using std::copy to temp vector using past-the-end iterator:
search<Person> s(db, "Select Name, Age from Persons");
search<Person> end_s;
std::vector<Person> v;
std::copy(s, end_s, back_inserter(v));
for (std::vector<Person>::iterator it = v.begin();it != v.end(); ++it)
std::cout << s->name_ << " " << s->age_ << std::endl;
// alternative 2.2
// Iteration using vector constructor with end() method:
search<Person> s(db, "Select Name, Age from Persons");
std::vector<Person> v(s, s.end());
for (std::vector<Person>::iterator it = v.begin();it != v.end(); ++it)
std::cout << s->name_ << " " << s->age_ << std::endl;
// alternative 2.3
// Iteration using std::copy to temporary vector using end() method:
search<Person> s(db, "Select Name, Age from Persons");
std::vector<Person> v;
std::copy(s, s.end(), back_inserter(v));
for (std::vector<Person>::iterator it = v.begin();it != v.end(); ++it)
std::cout << s->name_ << " " << s->age_ << std::endl;
// Alternative 3
// iteration using std::copy to copy directly to stdout
// (requires << operator is defined on Person)
search<Person> s(db, "Select Name, Age from Persons");
std::copy( s, s.end(), std::ostream_iterator<Person>( std::cout, "\n" ) );