c++ - 命令历史系统的最佳方法

标签 c++ allegro

好吧,我不确定如何在标题中解释我的问题,但基本上我想要实现的是使用 Allegro 的“命令行”式 GUI。图形工作正常,但由于显而易见的原因,保留历史记录的方法不起作用。我正在使用 map 来存储值,这对我来说真的很愚蠢。每次我向历史添加一个与以前的历史相同的命令时,以前的命令就会消失。我想知道的是,有没有一种方法可以让我存储值时不会像在 map 中那样被覆盖?

这是我目前的方法

我有一个名为 Point 的结构

struct Point {
    float x, y;

    Point() { this->x = 10.0; this->y = 440.0; }
    Point(float x, float y): x(x), y(y) { };
};

我用它来存储显示文本的点,我的程序的图形处理部分会使用这些点。

这是我在 HistoryManager.h 中定义的 HistoryManager 类

class HistoryManager {

    public:
        HistoryManager();
        ~HistoryManager();
        bool firstEntry;
        map<string, Point> history;

        void add_to_history(string);

    private:
        void update();
};

这里是 HistoryManager.cpp 中的定义

HistoryManager::HistoryManager() { this->firstEntry = false; }

HistoryManager::~HistoryManager() { }

void HistoryManager::add_to_history(string input) {

    if (!this->firstEntry) {
        this->history[input] = Point(10.0, 440.0);
        this->firstEntry = true;
    } else {
        this->update();
        this->history[input] = Point(10.0, 440.0);
    }
}

void HistoryManager::update() { 

    for (map<string, Point>::iterator i = this->history.begin(); i != this->history.end(); i++) {
        this->history[(*i).first] = Point((*i).second.x, (*i).second.y-10.0);
    }
}

我假设 vector 是一个选项,但有什么方法可以将这些值配对在一起吗?

最佳答案

使用std::pair

std::vector< std::pair <std::string, Point> > >

或者只是声明你自己的结构

struct HistoryEntry
{
    std::string input;
    Point point;
};

std::vector<HistoryEntry>

关于c++ - 命令历史系统的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389643/

相关文章:

c++ - 覆盖 Cpp 中的链接以指向模拟实现

c++ - 如何在C++中存储非常大的数字

c++ - 将另一个窗口嵌入为 QWidget

c++ - 将值从一个类传递到另一个类。单独的 cpp 文件。 C++

c - 在 osx 上使用 gcc 编译 allegro5 程序时出错

c++ - header 中的枚举导致过度重新编译

c++ - boost/输出到文件

c++ - 创建 Visual C++ 中缺少的 GUID 菜单项

c++ - 如何让一个程序在其他电脑上运行?

c++ - Allegro C++ 如何刷新删除行?