Home > Backend Development > C++ > How Can I Implement Switch Statements with Non-Integer Values in C/C ?

How Can I Implement Switch Statements with Non-Integer Values in C/C ?

Susan Sarandon
Release: 2024-11-30 06:28:11
Original
744 people have browsed it

How Can I Implement Switch Statements with Non-Integer Values in C/C  ?

C/C : Switch Statements for Non-Integral Values

In switch statements, it is often necessary to perform actions based on non-integer values. Unfortunately, in C/C , switch statements can only be used with integer expressions.

The Straightforward Approach

A simple solution for handling non-integral switch statements is to use a series of if-else statements:

if (str == "foo") {
  ...
} else if (str == "bar") {
  ...
} else {
  ...
}
Copy after login

However, this approach is verbose, inefficient (O(n), where n is the number of cases), and lacks the elegance of a switch statement.

Macro and Template Tricks

Using macros and templates, it is possible to implement binary search at compile time:

#define NEWMATCH
MATCH("asd")
  some c++ code
MATCH("bqr")
  ... the buffer for the match is in _buf
MATCH("zzz")
  ...  user.YOURSTUFF 
/*ELSE 
  optional
*/
ENDMATCH(xy_match)
Copy after login

This generates a function xy_match that automatically performs binary search and invokes the appropriate code for each case. This approach is efficient (O(log n)), but it is not as straightforward as a standard switch statement.

C 11 Enhancements

C 11 introduced lambdas and initializer lists, which greatly improve the clarity of non-integer switch statements:

template <typename KeyType, typename FunPtrType, typename Comp>
void Switch(const KeyType &amp;value,std::initializer_list<std::pair<const KeyType,FunPtrType>> sws,Comp comp) {
  auto r=std::lower_bound(sws.begin(),sws.end(),val,cmp);
  if ( (r!=sws.end())&amp;&amp;(!cmp(val,*r)) ) {
    r->second();
  }
}
Copy after login

This template provides a concise and efficient way to implement non-integer switch statements.

Compile Time Trie

A more recent advancement involves utilizing metaprogramming to generate a search trie at compile time. This approach handles unsorted non-integral switch statements and generates efficient code using the compiler's code generator:

Switch<const char *,void (*)()>("ger",{ // sorted: 
  {"asdf",[]{ printf("0\n"); }},
  {"bde",[]{ printf("1\n"); }},
  {"ger",[]{ printf("2\n"); }}
},[](const char *a,const char *b){ return strcmp(a,b)<0;}); 
Copy after login

This approach further enhances the efficiency and readability of non-integer switch statements in C/C .

The above is the detailed content of How Can I Implement Switch Statements with Non-Integer Values in C/C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template