Re: Need advice on data structure
nomad wrote:
I am writing a c++ program and will be implementing a console menu driven
prompt. The menu will have several options, and sub-menus may also have
multiple options. What is the best data structure to use? A tree? The STL
does not implement a tree, so I'd have to create my own.
Thanks
How about you don't think about the actual structure, and use classes
instead:
abstract class menuoption {
private:
std::string display;
std::string selectionValue;
protected:
menuoption(std::string display, std::string selectionValue) :
display(display), selectionValue(selectionValue) {}
public:
virtual void selected() = 0;
}
class menu : public menuoption {
private:
std::vector<menuoption> options;
public:
menu(std::string display, std::string selectionValue) :
menuoption(display, selectionValue) {}
void selected() {
/*
showMenuOptions();
menuoption &option = getUserResponse();
option.selected();
*/
}
}
Warning, if you're menus have circular references, you can get a stack
overflow with this method. If that is the case, a finite state machine
might be the way to go, but the model will look similar.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>