Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 861755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:01:49+00:00 2026-05-15T09:01:49+00:00

Ok, this may be really noobish question, but i am wishing there is something

  • 0

Ok, this may be really noobish question, but i am wishing there is something i dont know yet.

I go through a file, and check which string each line has, depending on the string value i execute a different function for it (or functions).

This is how i do it now:

Edit: I need to use variables outside the if-else-if range inside the if’s, updated code:

string s1 = "used";
string s2 = "in";
string s3 = "functions";

if(str == "something"){
    something = process(s1, s2);
}else if(str == "something else"){
    something = process(s2, s3);
}else if(str == "something more"){
    something = process(s1, s3);
    something = process(s1, s2);
}else if(str == "something again"){
    // do more stuff
}else if(str == "something different"){
    // do more stuff
}else if(str == "something really different"){
    // do more stuff
}

I am afraid this will become “slow” after i have to repeat those else if lines a lot…

I tried to use switch() statement, but obviously it doesnt work here, is there something similar to switch() to use here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:01:49+00:00Added an answer on May 15, 2026 at 9:01 am

    If you just want to execute different functions, you can use a map from strings to function pointers or functors like boost::function / tr1::function:

    void f1() { /* ... */ }
    void f2() { /* ... */ }
    
    // ... creating the map:
    typedef void (*FuncPtr)();
    typedef std::map<std::string, FuncPtr> FuncMap;
    FuncMap fnMap;
    fnMap["something"] = &f1;
    fnMap["something else"] = &f2;
    
    // ... using the map:
    FuncMap::const_iterator it = fnMap.find(str);
    if (it != fnMap.end()) { // is there an entry for str?
        it->second(); // call the function
    }
    

    As for passing parameters, with the details given so far i’d probably go for passing the unparsed remainder of the line or a list of string tokens to the functions and let them handle those as they see fit:

    void f1(const std::vector<std::string>& tokens) { /* ... */ }
    // ...
    
    typedef void (*FuncPtr)(const std::vector<std::string>&);
    typedef std::map<std::string, FuncPtr> FuncMap;
    // ...
    
    std::vector<std::string> tokens = /* ...? */; 
    FuncMap::const_iterator it = fnMap.find(str);
    if (it != fnMap.end()) { 
        it->second(tokens); 
    }
    

    You could also take a look at the interpreter example from Boost.FunctionTypes (header, source file) wether that is similar to your scenario.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a duplicate question, but really don't know how else to word
So, this may be a really stupid question, but I'm obviously missing something here.
I know this may be a really dumb question, but I'm testing out the
This may be a really silly question but it's something I've been struggling with.
This may sound really noobish, and I'm probably missing something really stupid.. But, I'm
this may be a really n00b question, but if your list of params contains
This may be a really silly question, but it just crossed my mind and
This may be a really dumb question but I'm just starting to learn the
This may be a really simple question, but I'm trying to create the database
This may be a long post but I really need to know how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.