469,086 Members | 1,216 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,086 developers. It's quick & easy.

Need advice on data structure

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
2 1496
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
"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.

Similar topics

reply views Thread by Doug R | last post: by
3 posts views Thread by Sigmathaar | last post: by
16 posts views Thread by C++ Hell | last post: by
8 posts views Thread by skumar434 | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.