Commit 9be3d764bf5d8d7b04139173f9c8d509224096ac

Authored by Grzegorz Jabłoński
1 parent d2e095ca

Using C++ 11-style loops for Employee example

examples05/01-employee/employee1.cpp
@@ -33,13 +33,8 @@ class Manager : public Employee { @@ -33,13 +33,8 @@ class Manager : public Employee {
33 33
34 void print_list(set<Employee*>& s) 34 void print_list(set<Employee*>& s)
35 { 35 {
36 - for (set<Employee*>::const_iterator p = s.begin(); p != s.end(); ++p)  
37 - (*p)->print();  
38 -}  
39 -  
40 -void print_list_2(set<Employee*>& s)  
41 -{  
42 - for_each(s.begin(), s.end(), mem_fun(&Employee::print)); 36 + for (Employee* p : s)
  37 + p->print();
43 } 38 }
44 39
45 int main() 40 int main()
@@ -49,5 +44,5 @@ int main() @@ -49,5 +44,5 @@ int main()
49 set<Employee*> empl; 44 set<Employee*> empl;
50 empl.insert(&e); 45 empl.insert(&e);
51 empl.insert(&m); 46 empl.insert(&m);
52 - print_list_2(empl); 47 + print_list(empl);
53 } 48 }
examples05/01-employee/employee2.cpp
@@ -34,15 +34,9 @@ class Manager : public Employee { @@ -34,15 +34,9 @@ class Manager : public Employee {
34 34
35 void print_list(set<Employee*>& s) 35 void print_list(set<Employee*>& s)
36 { 36 {
37 - for (set<Employee*>::const_iterator p = s.begin(); p != s.end(); ++p)  
38 - (*p)->print(); 37 + for (Employee* p : s)
  38 + p->print();
39 } 39 }
40 -  
41 -void print_list_2(set<Employee*>& s)  
42 -{  
43 - for_each(s.begin(), s.end(), mem_fun(&Employee::print));  
44 -}  
45 -  
46 int main() 40 int main()
47 { 41 {
48 Employee e("Brown", 1234); 42 Employee e("Brown", 1234);
@@ -50,5 +44,5 @@ int main() @@ -50,5 +44,5 @@ int main()
50 set<Employee*> empl; 44 set<Employee*> empl;
51 empl.insert(&e); 45 empl.insert(&e);
52 empl.insert(&m); 46 empl.insert(&m);
53 - print_list_2(empl); 47 + print_list(empl);
54 } 48 }