Jump to content
  • Advertisement
Sign in to follow this  
Knight52

best way to handling string command

This topic is 2255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My friend and I had an argument on how to handling string command. My friend said nested-if is the best, but I think storing command in Tree is better.

This is his nested if


if(command == "string1")
function1()
else if(command == "string2")
function2()
...


And this is my tree

pointer = head
for(i = 0; command != '\0'; i++)
pointer = pointer->child[command];
pointer->function();


I know a problem with tree is about allocating nodes, but I've worked around that by only allocate node that are needed and/or on the way to the nodes needed. What do you think? If there's a way better than these two I'd be appreciate to know too. Edited by Knight52

Share this post


Link to post
Share on other sites
Advertisement
One thing you could look at is using std::map for the command tree (that resolves some of the problems related to allocating nodes and space consumption), but normally I would use nested ifs unless there were a lot of commands.

Share this post


Link to post
Share on other sites

My friend and I had an argument on how to handling string command

There're several ways of doing it, if you really have lot of commands, you should consider to use a hashmap.


And this is my tree

This one is ok too, but how many commands are you expecting ? This solution is better for very large collection of words, like adictionary, else I would prefer a hashmap. Edited by Ashaman73

Share this post


Link to post
Share on other sites

class Command {
private:
std::string Name;
public:
virtual Function(const std::string & FullCommandLine);
};

std::map<std::string, Command*> CommandList;
// Calling
if (CommandList.find("SomeCommand")!=CommandList.end()) {
CommandList.second->Function(CommandLine);
} else {
// give some error CIERR0001: Command not found
}


Using the string in the command class is good for debugging purpose.

Only using STL Edited by Tribad

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!