我怎么能一个int映射到C / C对应的字符串++ [英] how can I map an int to a corresponding string in C/C++

查看:142
本文介绍了我怎么能一个int映射到C / C对应的字符串++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 20位,我想将它们与字符串相关联。有没有除了使用一个开关case语句来实现这个更快的方法。

我需要一个int转换为相应的字符串和数字不一定包装。有些code在的Qt 还有可能是有用的?

例如:下面的数字和字符串相互关联,

  1:请求系统信息2:更改系统信息10:未知错误


解决方案

使用地图更简单的方法

的std ::地图< INT,标准::字符串> MyMap中;
mymap中[1] =富;
mymap中[10] =酒吧;
// ...
INT IDX = 10;
标准::字符串查询= MyMap中[IDX];

I have 20 digits and I would like to associate them with strings. Is there a faster way besides using a switch case statement to achieve this.

I need to convert an int to a corresponding string and the numbers aren't necessarily packed. Some code in Qt as well might be useful?

Example: The following digits and strings are associated with each other,

1:   "Request System Info"

2:   "Change System Info"

10:  "Unkown Error"

解决方案

easier way to use map

std::map<int, std::string> mymap;
mymap[1] = "foo";
mymap[10] = "bar";
// ...
int idx = 10;
std::string lookup = mymap[idx];

这篇关于我怎么能一个int映射到C / C对应的字符串++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆