Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <list>
- #include <algorithm>
- #include <iostream>
- #include <string>
- using namespace std;
- class Editor {
- public:
- Editor(){
- it_=line_.begin();
- }
- void Left(){
- if (it_ ==line_.begin()) {
- return;
- }
- --it_;
- }
- void Right(){
- if (it_ ==line_.end()) {
- return;
- }
- ++it_;
- }
- void Insert(char token){
- line_.insert(it_, token);
- }
- void Cut(size_t tokens = 1){
- buffer_.clear();
- if (tokens > (size_t)(distance(it_, line_.end()))){
- tokens = (size_t)(distance(it_, line_.end()));
- }
- auto helper = std::next(it_, tokens);
- buffer_.assign(it_, helper);
- it_ = line_.erase(it_, helper);
- }
- void Copy(size_t tokens = 1){
- buffer_.clear();
- if (tokens > (size_t)(distance(it_, line_.end()))){
- tokens = (size_t)(distance(it_, line_.end()));
- }
- auto helper = std::next(it_, tokens);
- buffer_.assign(it_, helper);
- }
- void Paste(){
- for (char helper :buffer_) {
- it_ = line_.insert(it_, helper);
- ++it_;
- }
- }
- string GetText() const{
- return {line_.begin(), line_.end()};
- }
- private:
- list<char> buffer_;
- list<char> line_;
- list<char>::iterator it_;
- };
- int main() {
- Editor editor;
- const string text = "hello, world"s;
- for (char c : text) {
- editor.Insert(c);
- }
- for (size_t i = 0; i < text.size(); ++i) {
- editor.Left();
- }
- editor.Cut(7);
- for (size_t i = 0; i < 5; ++i) {
- editor.Right();
- }
- editor.Insert(',');
- editor.Insert(' ');
- editor.Paste();
- editor.Left();
- editor.Left();
- editor.Cut(3);
- cout << editor.GetText();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment