Реализуйте и протестируйте функцию Print_name() из параграфа 18.5.1 .
Писалось в книге что это очень легкое задание оценка (*1.5) , да фиг там оказалось на самом деле задание тяжелое, я его еле сделал, от в общем код.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
#include <iostream> using std::cout; using std::endl; using std::ostream; #include <string> using std::string; #include <list> using std::list; #include <functional> using std::unary_function; #include <algorithm> using std::find_if; #include <iterator> using std::back_inserter; #include <cstdlib> using std::exit; class Person { public: string name; string famili; Person(string n,string f):name(n),famili(f){} }; struct Club { string name; list<Person*> members; list<Person*> officers; //... Club(const string& n):name(n){} }; class Club_eq : public unary_function<Club, bool> { string s; public: explicit Club_eq(const string& ss):s(ss){} bool operator()(const Club& c){return c.name==s;} }; void f(list<Club>& lc) { typedef list<Club>::iterator LCI; LCI p=find_if(lc.begin(),lc.end(),Club_eq("three")); if(p!=lc.end()) cout <<(*p).name<<endl; else cout <<"not find"<<endl; //... } //класс извлечения class Extract_officers { list<Person*>& lst;//список персон public: explicit Extract_officers(list<Person*>& x) : lst(x){} void operator()(const Club& c)const { copy(c.officers.begin(),c.officers.end(),back_inserter(lst)); } }; //извлечение void extract(const list<Club>& lc, list<Person*>& off) { for_each(lc.begin(),lc.end(),Extract_officers(off)); } void Print_name(Person* i) { cout <<"mu tyt "<<i->name<<' '<<i->famili<<" dfasdf"<<endl; } void extract_and_print(const list<Club>& lc) { list<Person*> off; extract(lc,off); /*cout <<off.size()<<endl; list<Person*>::iterator it; for(it=off.begin();it!=off.end();++it) cout <<(*it)->name<<" kkk "<<endl; exit(1);*/ for_each(off.begin(),off.end(),Print_name); } int main() { list<Club> lc; Club one("one"); //Такс тут как мы видим нужно обязательно память выделять //а иначе она как бы временная становистя и пропадает. или хз. one.officers.push_back(new Person("mister","pupkin")); one.officers.push_back(new Person("mister1","pupkin1")); one.officers.push_back(new Person("mister2","pupkin2")); lc.push_back(one); lc.push_back(Club("two")); lc.push_back(Club("three")); lc.push_back(Club("four")); f(lc); extract_and_print(lc); return 0; } |
[youtube]http://www.youtube.com/watch?v=hJT0GovV3bI[/youtube]