Advertisement
Guest User

Untitled

a guest
Oct 10th, 2011
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4.  
  5. struct MyObject {
  6.     MyObject(int a, std::string b, bool c) : a(a), b(b), c(c) {}
  7.     int a;
  8.     std::string b;
  9.     bool c;
  10. };
  11.  
  12. //Vergleichsfunktion
  13. bool compare(MyObject const& obj1, MyObject const& obj2) {
  14.     return obj1.a < obj2.a;
  15. }
  16.  
  17. int main() {
  18.     std::list<MyObject> table;
  19.     table.push_back(MyObject(10, "Hello", false));
  20.     table.push_back(MyObject(5, "World", true));
  21.     table.push_back(MyObject(6, "noch", false));
  22.     table.push_back(MyObject(-5, "mehr", false));
  23.     table.push_back(MyObject(2, "Text", true));
  24.  
  25.     table.sort(compare);
  26.  
  27.     //Tabelle ausgeben
  28.     for (std::list<MyObject>::iterator iter = table.begin(); iter != table.end(); iter++) {
  29.         std::cout << iter->a << ", " << iter->b << ", " << (iter->c ? "true" : "false") << std::endl;
  30.     }
  31.     return 0;
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement