Many functions and choosing from a list

closed account (G1bS1hU5)
If i want to input a string and check for functions in a list that match the string
what´s the best way of doing it? ^^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getInput(){

std::string input;
std::cin >> input;

if(input=="something1"){
function1();

}
if(input=="something2"){
function2()

and so on
If you're comparing for string matches, a series of if-else statements is pretty much the only way to do it.

If you changed the method of input checking, you could make an array of std::functions or function pointers.

std::function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Example program
#include <iostream>
#include <string>
#include <functional>

void function0()
{
    std::cout << "error!" << std::endl;   
}

void function1()
{
    std::cout << "function 1" << std::endl;   
}

void function2()
{
    std::cout << "function 2" << std::endl;   
}

void function3()
{
    std::cout << "function 3" << std::endl;   
}

void getInput()
{
    typedef std::function<void()> Function;
    
    Function functions[] = { function0, function1, function2, function3 };
    
    int input = 0;
    std::cin >> input;
    
    // call the function at the input index
    functions[input]();
}

int main()
{
    getInput();
}


function pointer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Example program
#include <iostream>
#include <string>

void function0()
{
    std::cout << "error!" << std::endl;   
}

void function1()
{
    std::cout << "function 1" << std::endl;   
}

void function2()
{
    std::cout << "function 2" << std::endl;   
}

void function3()
{
    std::cout << "function 3" << std::endl;   
}

void getInput()
{
    typedef void(*Function)();
    Function functions[] = { function0, function1, function2, function3 };
    
    int input = 0;
    std::cin >> input;
    
    // call the function at the input index
    functions[input]();
}

int main()
{
    getInput();
}

that looks pretty solid to me. It is easy to read, maintain, and does what you want done.

you could use something more convoluted to avoid all the conditions -- eg a map of function pointers, such that input keys the correct function with no if statements. If you have a LOT of functions, something like that may clean up the code and be microscopically more efficient (string comparisons are slow relatively, and add up if you have a bunch).

you can rewrite it with a switch if you can make input an integer somehow.

you can do it with a class, but that would IMHO be very strange code to read and deal with. Something like a class with a string member that uses OOP tricks to call the correct function based off something wonky it did in the constructor from the string... but here we have one of those 'just because you can do something' approaches... this feels wrong to me.

Is there something you dislike about what you have?

Last edited on
Topic archived. No new replies allowed.