C++ 将字符串转换为变量名或变量类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7143120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string to variable name or variable type
提问by Rhexis
Is it possible to convert strings into variables(and vise versa) by doing something like:
是否可以通过执行以下操作将字符串转换为变量(反之亦然):
makeVariable("int", "count");
or
或者
string fruit;
cin >> fruit; // user inputs "apple"
makeVariable(fruit, "a green round object");
and then be able to just access it by doing something like:
然后可以通过执行以下操作来访问它:
cout << apple; //a green round object
Thanks in advance!
提前致谢!
回答by wilhelmtell
No, this is not possible. This sort of functionality is common in scripting languages like Ruby and Python, but C++ works very differently from those. In C++ we try to do as much of the program's work as we can at compile time. Sometimes we can do things at runtime, and even then good C++ programmers will find a way to do the work as early as compile time.
不,这是不可能的。这种功能在 Ruby 和 Python 等脚本语言中很常见,但 C++ 的工作方式与这些语言截然不同。在 C++ 中,我们尝试在编译时尽可能多地完成程序的工作。有时我们可以在运行时做一些事情,即使这样,优秀的 C++ 程序员也会在编译时找到一种方法来完成这项工作。
If you know you're going to create a variable then create it right away:
如果您知道要创建一个变量,请立即创建它:
int count;
What you might not know ahead of time is the variable's value, so you can defer that for runtime:
您可能事先不知道的是变量的值,因此您可以将其推迟到运行时:
std::cin >> count;
If you know you're going to need a collection of variables but not precisely how many of them then create a mapor a vector:
如果您知道将需要一组变量,但不知道具体有多少个变量,则创建一个map或一个vector:
std::vector<int> counts;
Remember that the name of a variable is nothing but a name — a way for you to refer to the variable later. In C++ it is not possiblenoruseful to postpone assigning the name of the variable at runtime. All that would do is make your code more complicated and your program slower.
请记住,变量的名称只不过是一个名称——这是您以后引用该变量的一种方式。在 C++ 中,在运行时推迟分配变量的名称是不可能也没有用的。这样做只会使您的代码更复杂,程序更慢。
回答by sje397
You can use a map.
您可以使用地图。
map<string, int> numbers;
numbers["count"] = 6;
cout << numbers["count"];
回答by Ernest Friedman-Hill
Beginning programmers ask this question regarding every language. There are a group of computer languages for which the answer to this question is "yes". These are dynamic, interactive languages, like BASIC, Lisp, Ruby, Python. But think about it: Variable names only exist in code, for the convenience of the programmer. It only makes sense to define a new variable while the program runs if there's a person to then subsequently type the name of the variable in new code. This is true for interactive language environment, and not true for compiled languages like C++ or Java. In C++, by the time the program runs, and the imaginary new variable would be created, there's no one around to type code that would usethat new variable.
初学者对每种语言都会问这个问题。有一组计算机语言对此问题的回答是“是”。这些是动态的交互式语言,如 BASIC、Lisp、Ruby、Python。但是想一想:变量名只存在于代码中,为了程序员的方便。如果有人随后在新代码中键入变量的名称,那么在程序运行时定义新变量才有意义。这适用于交互式语言环境,不适用于 C++ 或 Java 等编译语言。在 C++ 中,当程序运行时,将创建假想的新变量,没有人可以键入使用该新变量的代码。
What you really want instead is the ability to associate a name with an object at runtime, so that code -- not people -- can use that name to find the object. As other people have already pointed out, the map
feature of C++'s standard library gives you that ability.
相反,您真正想要的是能够在运行时将名称与对象相关联,以便代码(而不是人)可以使用该名称来查找对象。正如其他人已经指出的那样,map
C++ 标准库的特性为您提供了这种能力。
回答by feathj
No. C++ is statically typed, and this goes against that whole paradigm.
不。C++ 是静态类型的,这与整个范式背道而驰。
I have seen this type of functionality implemented before by storing variables in an stl map.
我以前见过这种类型的功能是通过在 stl 映射中存储变量来实现的。
回答by math
At least for the (vice versa) there is a possibility with the preprocessor statement stringify #
. See this answeron how to convert a C++ variable name into an string.
至少对于(反之亦然)预处理器语句 stringify 是有可能的#
。请参阅有关如何将 C++ 变量名称转换为字符串的答案。
回答by Murtaza
well i guess u cannot make dynamics variables but u can use some function to write a new variable and its value in any external text file and access its value from that file where ever it is needed (u can also remove the dynamic variable by removing it from the text file.)
好吧,我猜你不能创建动态变量,但你可以使用一些函数在任何外部文本文件中写入一个新变量及其值,并在需要时从该文件访问它的值(你也可以通过删除动态变量来删除它来自文本文件。)
theory: variables are places in memory where we store data, identified by a name, we can store data in a text file if processor doesnot allow us to store it in registers, and we can access its value by searching its identity (name of variable) int the text file, our data will be next to it.
理论:变量是内存中我们存储数据的地方,由名称标识,如果处理器不允许我们将数据存储在寄存器中,我们可以将数据存储在文本文件中,我们可以通过搜索其身份(变量名称)来访问其值) 在文本文件中,我们的数据将在它旁边。
its just an idea, it should work but i guess it will be not very simple and ur program will have to pay in terms of speed.
这只是一个想法,它应该可行,但我想它不会很简单,你的程序将不得不在速度方面付出代价。