Home > Back-end >  Refer to the switch case alternatives
Refer to the switch case alternatives

Time:10-30

The service side, handle the client request, different request types corresponding to different functions, there are hundreds of types, feeling some of the switch case is a bit tedious, want to replace the function map, but under test, the function of map efficiency is lower than the switch case too much, later want to change the function array, made the subscript request type, but the request type is not continuous, don't know how to initialize the array before operation, all bosses still have what way?

CodePudding user response:

Change the function map to function hash

CodePudding user response:

The switch case compiler has been optimized is very good,

CodePudding user response:

Can you define a function pointer array, the type definition into the array in the following table, of course, in the following table and function need one to one correspondence, for your reference

CodePudding user response:

Direct use of polymorphism

CodePudding user response:

references in 4th floor, the truth is more important than right or wrong response:
direct use polymorphic.
polymorphism is too much trouble

CodePudding user response:

reference 3 building self-confidence boy reply:
can you define a function pointer array, the type definition into the array in the table below, of course, in the following table and function needs one to one correspondence, for your reference ~
that's what I'm currently processing, but also got new members of a static class to initialize the array, are at odds

CodePudding user response:

refer to the second floor 4 teacher zhao response:
switch case compiler has been optimized is very good,

Tested, the switch case than the if - else if - fast,,, why you want to change
If the agreement is too much, can write their own tools, automatic code generation agreement,

CodePudding user response:

Polymorphic don't bother, it is because of you there are hundreds of types, use polymorphic
fit

CodePudding user response:

The class Base {
Public:
Virtual func ()=0;
}
The class ch1: Base {
Public:
Func () {}
};
The class ch2: Base {
.
}

VectorFor (auto o: list)
{
O - & gt; Func ();
}

CodePudding user response:

The original poster can use unordered_map instead, has several advantages:
1. The first is for internal use hash table hash algorithm, fast absolute
2. Can realize array access method, at the same time, breakthrough the limitation of the array, the array subscript must be digital, and unordered_map key can be a string or a complex object,
3. The initialization and use similar to c + + array, and add elements is very convenient, show as follows:

According to the need of you, you can let the type as the key, and let the function pointer as the value, in this way to add or use
Unordered_map & lt; KeyType, valueType> FunArr;
FunArr [key]=value;/insert/array form, if the container don't have the key, can add the key and the value, if any, will update his value

  • Related