2013-05-05 54 views
0

好的,我不確定如何在標題中解釋我的問題,但基本上我試圖實現的是使用Allegro的「命令行」-esque GUI。圖形工作正常,但保持歷史的方法不起作用的原因很明顯。我使用地圖來存儲我開始時真正愚蠢的值。每當我向歷史記錄中添加一條與先前歷史記錄相同的命令時,前一個歷史記錄消失。我想知道的是,是否有一種方法可以將值存儲爲不會像地圖中那樣覆蓋的值?命令歷史記錄系統的最佳途徑

這裏是我當前的方法

我有一個名爲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

class HistoryManager { 

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

     void add_to_history(string); 

    private: 
     void update(); 
}; 

定義我HistoryManager註冊類並在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); 
    } 
} 

我假設向​​量defenitions是一個選項,但有沒有配對的任何方式價值在一起?

回答

1

使用std::pair

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

或者只是聲明自己的結構

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

std::vector<HistoryEntry> 
+0

可以說,我用'的std :: pair',我將如何訪問Point結構? – PurityLake 2013-05-05 22:15:25

+0

'vec [i] .second' – john 2013-05-05 22:15:46

+0

非常感謝約翰 – PurityLake 2013-05-05 22:16:26