shushus

Спринт 8. Эффективный список.

Feb 2nd, 2023
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <list>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Editor {
  9. public:
  10.     Editor(){
  11.         it_=line_.begin();
  12.     }
  13.  
  14.     void Left(){
  15.         if (it_ ==line_.begin()) {
  16.             return;
  17.         }
  18.         --it_;
  19.     }  
  20.    
  21.     void Right(){
  22.         if (it_ ==line_.end()) {
  23.             return;
  24.         }
  25.         ++it_;
  26.     }
  27.  
  28.     void Insert(char token){
  29.         line_.insert(it_, token);
  30.     }
  31.  
  32.     void Cut(size_t tokens = 1){
  33.         buffer_.clear();
  34.         if (tokens > (size_t)(distance(it_, line_.end()))){
  35.             tokens = (size_t)(distance(it_, line_.end()));
  36.         }
  37.         auto helper = std::next(it_, tokens);
  38.         buffer_.assign(it_, helper);
  39.         it_ = line_.erase(it_, helper);
  40.     }
  41.  
  42.     void Copy(size_t tokens = 1){
  43.         buffer_.clear();
  44.         if (tokens > (size_t)(distance(it_, line_.end()))){
  45.             tokens = (size_t)(distance(it_, line_.end()));
  46.         }
  47.         auto helper = std::next(it_, tokens);
  48.         buffer_.assign(it_, helper);
  49.     }
  50.  
  51.     void Paste(){
  52.         for (char helper :buffer_) {
  53.             it_ = line_.insert(it_, helper);
  54.             ++it_;
  55.         }
  56.     }
  57.  
  58.     string GetText() const{
  59.         return {line_.begin(), line_.end()};
  60.     }
  61. private:
  62.     list<char> buffer_;
  63.     list<char> line_;
  64.     list<char>::iterator it_;
  65. };
  66.  
  67. int main() {
  68.     Editor editor;
  69.     const string text = "hello, world"s;
  70.     for (char c : text) {
  71.         editor.Insert(c);
  72.     }
  73.  
  74.     for (size_t i = 0; i < text.size(); ++i) {
  75.         editor.Left();
  76.     }
  77.  
  78.     editor.Cut(7);
  79.  
  80.     for (size_t i = 0; i < 5; ++i) {
  81.         editor.Right();
  82.     }
  83.  
  84.     editor.Insert(',');
  85.     editor.Insert(' ');
  86.  
  87.     editor.Paste();
  88.  
  89.     editor.Left();
  90.     editor.Left();
  91.  
  92.     editor.Cut(3);
  93.  
  94.     cout << editor.GetText();
  95.    
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment