我写了一个文本编辑器程序在C++中有简单的命令:LEFT,RIGHT,HOME,END,BACKSPACE,DELETE,INSERT,现在我需要执行UNDO和REDO功能。在我的程序中,用户必须能够撤消最多十个命令。我想使用矢量实现来实现这一点,但我不知道如何设置它。我不知道如何将光标位置和字符存储到向量中。有人可以提供一些帮助吗?需要帮助编写撤消/重做功能的文本编辑器程序
#ifndef CURSOR_H
#define CURSOR_H
#include <stdlib.h>
#include <iostream>
template <class Object>
class Cursor;
// Incomplete Declaration
template <class Object>
class CNode
{
public:
CNode(const Object & theElement = Object(), CNode * n = NULL) : element(theElement), next(n) { }
Object element;
CNode *next;
friend class Cursor<Object>;
};
template <class Object>
class Cursor
{
public:
Cursor();
bool isEmpty() const;
void makeEmpty();
void left ();
void right ();
void del (); //This is the delete operation. I named it del instead of delete as delete conflicts with a C++ keyword.
void back ();
void insert(const Object & x);
void home ();
void end ();
void undo ();
private:
void printText () ;
CNode<Object> *header;
CNode<Object> *cursorPosition;
};
//#include "Cursor.cpp"
#endif
一个环形缓冲区将允许相同的动作,而不需要任何分配/解除分配。 – 2010-10-05 02:40:36
@本,非常真实。但是环形缓冲区不是标准的一部分,我发现了一个渴望与标准容器保持一致的愿望;也许我错了。 Boost提供了一个:http://www.boost.org/doc/libs/1_37_0/libs/circular_buffer/doc/circular_buffer.html – 2010-10-05 02:45:43
“检测出坚持使用标准容器的愿望”:CNode <>是对相反,虽然提到了vector <>。 – 2010-10-05 02:55:51