使用字符串变量来调用其他东西并为其命名

使用字符串变量来调用其他东西并为其命名

本文介绍了C++ 使用字符串变量来调用其他东西并为其命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用字符串变量为新变量或对象命名?

How can I give a name to a new variable or object using a string variable?

例如:在我编译程序后,我输入文本a_name",按[Enter],然后一个名为a_name"的变量或对象被声明.另一个例子:我输入文本a_name",按[Enter],然后名为a_name"的变量显示它的值.

For example: After I compile the program, I enter the text "a_name", press [Enter] and then a vaiable or object with the name "a_name" gets declared. Another example: I enter the text "a_name", press [Enter] and then the variable called "a_name" shows it's value.

是否有任何特殊的库需要下载?或者有没有包含在编译器文件中的?如果有必须下载的库,哪个最容易理解和使用?我使用的是 Visual C++,但使用的是库 Iostream、Math、String 等.复制自 DevC++ 编译器.

Are there any special Libraries for this, that have to be downloaded? Or are there ones that are included in the compilers files? If there are libraries that have to be downloaded, which are the easiest to understand and use? I'm using Visual C++, but with the Libraries Iostream, Math, String, e.t.c. copied from the DevC++ compiler.

推荐答案

一旦程序被编译,就不能向程序添加变量".你但是,通过使用 std::map 可以获得或多或少相同的效果,一个字符串作为键类型.

You can't add "variables" to a program once it has been compiled. Youcan get more or less the same effect, however, by using std::map, witha string as the key type.

当然,您必须决定新变量的类型有,所以你知道将它映射到什么类型.如果可能有不止一种类型,像 boost::variant 这样的东西可能会有用.(请注意,与名称集不同,可能类型集必须在编译时完全定义.)

You will, of course, have to decide what type the new variable shouldhave, so you know what type to map it to. If there may be more than one type, something like boost::variant might be useful. (Note that unlike the set of names, the set of possible types must be completely defined at compile-time.)

这篇关于C++ 使用字符串变量来调用其他东西并为其命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 14:20