STL - copy i ostream_iterator
Ostatnio zmodyfikowano 2017-02-21 18:50
croppp Temat założony przez niniejszego użytkownika |
STL - copy i ostream_iterator » 2017-02-21 18:22:53 Hej, czy jest możliwość wyświetlenia zawartości wektora w ten sposób jak zrobiłem to w ostatniej linijce? #include <iostream> #include <vector> #include <iterator> #include <string> #include <algorithm>
using namespace std;
class Tperson { private: string f_name; string l_name; int age; public: Tperson( string first_name = "default", string last_name = "default", int how_old = 0 ) : f_name( first_name ) , l_name( last_name ) , age( how_old ) { } ~Tperson() { }; string & get_fname() { return f_name; } string & get_lname() { return l_name; } int get_age() { return age; } };
void Show( Tperson & prs ) { cout << "Imie " << prs.get_fname() << " Nazwisko " << prs.get_lname() << " Wiek " << prs.get_age() << endl; }
bool operator <( Tperson & prs1, Tperson & prs2 ) { if( prs1.get_lname() > prs2.get_lname() ) return true; else return false; }
ostream & operator <<( ostream & os, Tperson & prs ) { os << prs.get_fname() << endl; return os; }
int main() { vector < Tperson > people; people.push_back( Tperson( "Michal", "xxxx", 29 ) ); people.push_back( Tperson( "Magalena", "uuuu", 32 ) ); people.push_back( Tperson( "Agnieszka", "iii", 21 ) ); people.push_back( Tperson( "Magda", "yyyyy", 25 ) ); for_each( people.begin(), people.end(), Show ); cout << endl; sort( people.begin(), people.end() ); cout << endl; for_each( people.begin(), people.end(), Show ); cout << endl; vector < Tperson > people2; people2.resize( people.size() ); copy( people.begin(), people.end(), people2.begin() ); for_each( people2.begin(), people2.end(), Show ); copy( people2.begin(), people2.end(), ostream_iterator < Tperson, string >( cout ) ); return 0; }
|
|
Elaine |
» 2017-02-21 18:31:11 Drugi parametr szablonu ostream_iterator powinien pozostać przy wartości domyślnej: ostream_iterator<Tperson>(cout). |
|
croppp Temat założony przez niniejszego użytkownika |
» 2017-02-21 18:35:01 Niestety nie działa; no match for 'operator<<' |
|
Elaine |
» 2017-02-21 18:47:06 Twój operator<< powinien przyjmować const Tperson&. |
|
croppp Temat założony przez niniejszego użytkownika |
» 2017-02-21 18:50:03 ... no oczywiście, że tak... Dzięki za pomoc. |
|
« 1 » |