Reputation: 45
I have the task of getting the number of the variable y
depending on which number worked in first_number
and second_number
My version is working, but I have to constantly repeat the switch
, in the end I have a lot of code, I'm very uncomfortable.
The problem is that the variable is different everywhere and I don’t know how to create a function where I can specify a number for each case
block
switch (first_number)
{
case 0:
{
switch (second_number)
{
case 0:
{
y = 13;
cout << y << endl;
}
break;
case 1:
{
y = 15;
cout << y << endl;
}
break;
}
}
break;
case 1:
{
switch (second_number)
{
case 0:
{
y = 3;
cout << y << endl;
}
break;
case 1:
{
y = 1;
cout << y << endl;
}
break;
}
}
break;
case 2:
{
switch (second_number)
{
case 0:
{
y = 156;
cout << y << endl;
}
break;
case 1:
{
y = 67;
cout << y << endl;
}
break;
}
}
break;
}
Upvotes: 1
Views: 89
Reputation: 66459
Encode the function as a table.
const int table[3][2] = {{13,15}, {3,1}, {156,67}};
int y = table[first_number][second_number];
Upvotes: 6
Reputation: 123114
There are many ways to simplify this. My goto solution often is std::map
. It often isn't the most efficient container to choose, but it is extremely useful and performance is for later, when I know that I need to do something about it.
I dont fully understand what your code is supposed to do (also because some important details are missing), but I think you can use something like:
#include <map>
#include <utility>
#include <iostream>
void foo(std::pair<int,int> x) {
static std::map<std::pair<int,int>,int> lookup_table{
{ { 1,1 }, 1},
{ { 2,3 }, 42}
};
std::cout << lookup_table[x] << '\n';
// or
auto it = lookup_table.find(x);
if (it != lookup_table.end()) {
std::cout << it->second << '\n';
}
}
int main(){
foo( {1,1} );
}
The first variant, using operator[]
will create an element in the map when the key cannot be found. I don't think this is what you want, so I included the variant using find
that lets you check if an element for the given key exists in the map (and would also allow you to declare the lookup_table
as const
).
Last but not least, note that the above is far from being optimal. In the next step I would maybe consider to use an array and try to avoid the function local static
. Nevertheless thats what would be my first approach.
Upvotes: 3
Reputation: 597
Simply put, you can't.
Imagine all your outputs are a, b, c, d, e, f
. Each of your outputs requires a unique combination of inputs to result in one of the outputs. For example
0,0: a
0,1: b
1,0: c
1,1: d
2,0: e
2,1: f
Because all of your outputs are unique, the paths taken to them must also be unique. A fundamental thing with function logic is that the same inputs cannot have two different outputs. Here, you cannot have any reduction in combinations of inputs, because each of your outputs are unique, and thus having a shorter list of inputs would violate that rule.
This isn't barring something like a key-value map to reduce your code, but you won't be able to reduce the number of combinations needed to retrieve those values.
Upvotes: 1