fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class Book {
  7. private:
  8. int id;
  9. string title;
  10. public:
  11. Book(int i, string t) { id = i; title = t; }
  12. ~Book() {};
  13. int getId() { return id; }
  14. string getTitle() { return title; }
  15. };
  16.  
  17. int main(void){
  18. vector<Book> bookShelf;
  19. bookShelf.push_back(Book(1, "English"));
  20. bookShelf.push_back(Book(2, "Math"));
  21. bookShelf.push_back(Book(3, "History"));
  22. bookShelf.push_back(Book(4, "Physics"));
  23. bookShelf.push_back(Book(5, "Programming"));
  24. bookShelf.push_back(Book(6, "Science"));
  25.  
  26. cout << "------- Order -------" << endl;
  27.  
  28. for (auto& book : bookShelf)
  29. cout << "id: " << book.getId() << " title: " << book.getTitle() << endl;
  30.  
  31. cout << "------- Reverse Order -------" << endl;
  32.  
  33. for (auto ite = bookShelf.rbegin(); ite != bookShelf.rend(); ite++)
  34. cout << "id: " << (*ite).getId() << " title: " << (*ite).getTitle() << endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
------- Order -------
id: 1 title: English
id: 2 title: Math
id: 3 title: History
id: 4 title: Physics
id: 5 title: Programming
id: 6 title: Science
------- Reverse Order -------
id: 6 title: Science
id: 5 title: Programming
id: 4 title: Physics
id: 3 title: History
id: 2 title: Math
id: 1 title: English