×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Remove redundancies

Remove redundancies

Remove redundancies

(OP)
Hi, I have the following problem

CODE

#define FIRST_OPTION 1
#define SECOND_OPTION 2
#define THIRD_OPTION 3
#define FOURTH_OPTION 4
Then I have some STL input string, let’s say inpStr and do sth like that

CODE

if (inpStr == "FIRST_OPTION")
   importantVariable = FIRST_OPTION;
else if (inpStr == "SECOND_OPTION")
   importantVariable = SECOND_OPTION;
...
else
   importantVariable = UNKNOWN_OPTION;
After such checking code becomes more and more redundant, however, I cannot find a way to do this smarter.  I’m wondering if we can apply some casting to do this more less that simple (pseudo code)

CODE

checkIfDefineExists(inpStr)
if YES:
   importantVariable = (getIntegerValueFromDefinedHavingName)inpStr;
if NO:
   importantVariable = -1
Any help will be appreciated.

RE: Remove redundancies

An alternative would be to put in in a map.

CODE

#include <map>

typedef std::map<std::string, int> MyMap;
// ...
{
  // Setup Map
  MyMap map;
  map["1ST_OPTION"] = 1;
  map["2ND_OPTION"] = 2;
  map["3RD_OPTION"] = 3;
  // etc.

  // ...

  // Query for existance/value
  MyMap::iterator it=map.find(inpStr);
  if (it ! map.end())
  {
    // Found it
    int value = (*it).second;
  }
  else
  {
    // inpStr not found
  }
}

Code usually get a lott simpler/less cluttered if you "mapify" choces like this", specially if it's a lot of options to choose from.

If it actually is a huge amount of options to choose from another benefit would kick in; that the map's find works in logarithmic time and which is a lot faster than the if statemens linear time.

/Per

www.perfnurt.se

RE: Remove redundancies

(OP)
That is really  good idea, however, what to do if those defines comes from some include files that cannot be changed. Of course I can perform mapping of those defines

CODE

#include fileWithDefines.h
map["1ST_OPTION"] = 1ST_OPTION;
and I believe it would be much improve but maybe there is another method.

RE: Remove redundancies

Since I'm a bit allergic to macros (I would even suggest replacing your current macros with const int) I probably shouldn't suggest this...but anyway...

You could define a macro that uses the stringify prepocessor operator (#), like this:

CODE

#define ASSIGN_TO_MAP(map, value) map[#value]=value

Then you can set up the map like this

CODE

MyMap map;
ASSIGN_TO_MAP(map, FIRST_OPTION);
ASSIGN_TO_MAP(map, SECOND_OPTION);
ASSIGN_TO_MAP(map, THIRD_OPTION);
//etc ...

/*
   ASSIGN_TO_MAP(map, FIRST_OPTION);
   expands to
   map["FIRST_OPTION"]=FIRST_OPTION;
*/


/Per

www.perfnurt.se

RE: Remove redundancies

If all those #defines are related, I'd change them to an enum.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close