423,485 Members | 1,664 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,485 IT Pros & Developers. It's quick & easy.

Need advice on data structure

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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<menuoptionoptions;
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/>
Jun 27 '08 #2

P: n/a
"nomad" writes:
>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.
The wizards that wrote the STL spec decided to use the word "map" where a
more earthy type would call this thing a tree. See the drawing and text on
p 195 of Josuttis for example. That book is essential for any serious user
of the STL. It is not just a reference book, it is also an excellent
tutorial on the STL.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.