当前位置:网站首页>How to use string branches for switch case
How to use string branches for switch case
2022-06-30 07:09:00 【HL_ Aeolus】
switch-case As stipulated in the grammar case You can't use a string case The branch , Sometimes there may be relevant requirements in the project
//common.h
#include <stdint.h>
using namespace std;
namespace common
{
constexpr uint32_t strToHash(const char* str);
uint32_t add(uint32_t a, uint32_t b);
};
constexpr uint32_t common::strToHash(const char* str)
{
uint32_t val(0);
for (int i = 0; str && ('\0' != str[i]); i++)
val = (val << 6) ^ (val >> 26) ^ str[i];
return val;
}
//main.cpp
#include <iostream>
#include <vector>
#include "common.h"
#include <string>
#include <stdio.h>
#define CASE(input,func) \ case common::strToHash(input): \ func; \ break;
void myDebug(string str)
{
cout << str << ",hash value :" << common::strToHash(str.c_str()) << endl;
}
void SwitchByString(string str)
{
if (str.size() == 0)
{
return;
}
const char* input = str.c_str(); //string convert to char *
switch (common::strToHash(input))
{
CASE("0x0002", myDebug("0x0002"))
default:
break;
}
}
int main()
{
SwitchByString("0x0002");
system("pause");
return 0;
}

边栏推荐
- Traverse map
- Go常用命令
- Write and run the first go language program
- Go common commands
- 踩坑记录:supervisor 日志返回信息:redis扩展未安装
- If I am in Zhuhai, where can I open an account? In addition, is it safe to open a mobile account?
- js创建pdf文件
- app quits unexpectedly
- [mask RCNN] target detection and recognition based on mask RCNN
- What if I forget my account number after opening an account? Is it safe to open an account online?
猜你喜欢

Google Earth engine (GEE) - Murray global tidal wetland change V1 (1999-2019) data set

Deploying web projects using idea

踩坑记录:supervisor 日志返回信息:redis扩展未安装

Pit stepping record: Supervisor log return information: redis extension is not installed
![[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network](/img/39/c9bf235828b6d50e7931876fa4a918.png)
[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network

Definition and use of ROS topic messages

Record one time of Tencent Test Development Engineer's automation interface test practice experience

The first up Master of station B paid to watch the video still came! Price "Persuading" netizens

【最全】linux服务器上安装Mysql

Go installation and configuration (1)
随机推荐
Egret P2 physical engine (1) small ball falling demo
Idea shortcut key
Deploying web projects using idea
Mysql5.7 compressed version installation tutorial
Performance comparison of random network, scale-free network, small world network and NS small world matlab simulation
Relevant database questions.
[datawhale team learning] task02: mathematical operation, string and text, list
[Hot100]10. Regular Expression Matching
社招两年半10个公司28轮面试面经
Go常用命令
[daily question] 535 Encryption and decryption of tinyurl
June 29, 2022 -- take the first step with C # -- add decision logic to the code using the "if", "else" and "else if" statements in C #
SQL Server2005中SUM函数内嵌套IF语句
编写并运行第一个Go语言程序
Google Earth Engine(GEE)——墨累全球潮汐湿地变化 v1 (1999-2019) 数据集
[docsify basic use]
Linux服務器安裝Redis
【json-tutorial】第一章学习笔记
年轻人搞副业有多疯狂:月薪3000,副业收入3W
Golan common shortcut key settings