Advertisement
i1last

future code, homework, 4.6.1. Memento

May 16th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <vector>
  2.  
  3. class Memento {
  4.     friend class CustomVector;
  5. private:
  6.     std::vector<int> state;
  7.    
  8.     Memento(const std::vector<int>& vec) : state(vec) {}
  9. };
  10.  
  11. class CustomVector {
  12. private:
  13.     std::vector<int> data;
  14.    
  15. public:
  16.     void PushBack(int value) {
  17.         data.push_back(value);
  18.     }
  19.    
  20.     void PopBack() {
  21.         if (!data.empty()) {
  22.             data.pop_back();
  23.         }
  24.     }
  25.    
  26.     void Set(int index, int value) {
  27.         if (index >= 0 && index < data.size()) {
  28.             data[index] = value;
  29.         }
  30.     }
  31.    
  32.     int Get(int index) const {
  33.         if (index >= 0 && index < data.size()) {
  34.             return data[index];
  35.         }
  36.         return -1;
  37.     }
  38.    
  39.     Memento CreateMemento() {
  40.         return Memento(data);
  41.     }
  42.    
  43.     void Restore(const Memento& memento) {
  44.         data = memento.state;
  45.     }
  46. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement