动态变量名称。请帮助 [英] Dynamic variable name. plz help

查看:74
本文介绍了动态变量名称。请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何在给定此名称的字符串的情况下指定变量的名称(也称为

标识符)。例如:


string s =" whatever";


如何获得以下表达式:

double whatever = 4;


如你所见,我希望变量名取自字符串的值。


这让我很难过。

thx很多

I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string''s value.

This is killing me.
thx alot

推荐答案

cpp写道:
我是试图找出如何在给定该名称的字符串的情况下指定变量的名称(也称为
标识符)。例如:

string s =" whatever" ;;

如何获得以下表达式:
double whatever = 4;

这就是杀了我。
很多
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string''s value.

This is killing me.
thx alot




为什么不使用std :: map< std :: string,double>?


#include< iostream>

#include< string>

#include< map>

int main()

{

std :: map< std :: string,double> mymap;

std :: string s(" whatever");


mymap [std :: string(" whatever")] = 4;

std :: cout<< mymap [s]<< std :: endl;


返回0;

}



Why not use std::map<std::string, double>?

#include <iostream>
#include <string>
#include <map>
int main()
{
std::map<std::string, double> mymap;
std::string s("whatever");

mymap[std::string("whatever")] = 4;
std::cout << mymap[s] << std::endl;

return 0;
}





cpp写道:


cpp wrote:
我试图找出如何在给定字符串的情况下指定变量的名称(也称为
标识符)这个名字。例如:

string s =" whatever" ;;

如何获得以下表达式:
double whatever = 4;
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string''s value.



我很确定这不可能像你''那样'重新考虑因为

s的值在运行时已知(或者可能在运行时更改)

而变量名需要在编译时知道。但是如果

您只是尝试使用具有相同标识符的变量名称和字符串

a宏可能有助于只传递一个标识符但创建一个字符串
$ b带有该值的$ b和具有该名称的变量:


#define MyMacro(Identifier)\

string s = #Identifier; \

双标识符= 4;


所以代码

MyMacro(无论如何)

会完全按照你上面输入的内容。


I''m pretty sure this is not possible the way you''re thinking of since
the value of s is known at runtime (or might change during runtime)
while the variable name needs to be known at compile time. However if
you just try to have a variable name and string with the same identifier
a macro might help passing only one identifier to but creating a string
with that value and a variable with that name:

#define MyMacro(Identifier) \
string s = #Identifier; \
double Identifier = 4;

so the code
MyMacro(whatever)
would do exactly what you typed above.


cpp写道:

我试图找出如何给定此名称的字符串,指定变量的名称(也称为
标识符)。例如:

string s =" whatever" ;;

如何获得以下表达式:
double whatever = 4;

正如你所看到的,我希望变量名取自字符串的值。

这就是杀了我。

I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string''s value.

This is killing me.



好​​消息!你可以不用担心。一般来说你不能这样做。

可能是平台特定的方式进行符号查找(假设事情甚至是b $ b编译为符号)但这是非常不可移植的。


为什么不告诉我们你实际上在做什么,而不是说b $ b描述你的问题解决方案失败?


Brian Rodenborn


Good news! You can stop worrying. You can''t do it in general. There may
be platform specific ways to do symbol lookup (assuming things were even
compiled to have symbols) but that''s highly unportable.

Why don''t you tell us what you are actually trying do, rather than
describing your failed solution to the problem?

Brian Rodenborn


这篇关于动态变量名称。请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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