如何通过名称存储在另一个变量(C ++) [英] How to adress variable by name stored in another variable (C++)

查看:125
本文介绍了如何通过名称存储在另一个变量(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,



我想通过使用变量来存储其名称来引用函数中的结构。这是可能在C ++中做这样的事吗?



当然,所有现有的结构将在任何调用之前被声明和初始化(可能是全局的)



我想要这种精神的东西:

  struct StructName 
{
... stuff
} a,b,c;

StructName a;
StructName b;



等。包括设置所需的值(根据需要在初始化或代码中的其他地方)



,然后我会有类似这样从另一部分代码调用:

  void myFunction(char someInput)
{
一些东西
一些东西
externalFunction ,static parameter,_someInput_,static parameter);
还有一些其他东西
}

其中somInput是a,b或c。

请记住,我是C的初学者,很少或没有正式的主题训练。



谢谢。



编辑:如果它只是我自己,我会为someInput做case切换,直接引用结构,的代码意味着可以由非程序员扩展,该程序员将提供结构本身,我将向他提供结构初始化代码的模板,并且他将把初始化代码添加到代码中的指定位置,修改列表

解决方案

变量名称消失的C和C ++中的编译步骤。



通常,有两种方案可以解决您描述的问题类型:


  1. 用户输入对应于特定的变量名称。

  2. 您实际上不想要变量的名称,但只需要一种方法将不同的数据与代码的不同部分相关联。

在第二种更简单的情况下,只需使用数组,并使用索引到您想要的元素正确的数据。



在第一种情况下,您可以使用 std :: map< std :: string,structname&> std :: map 的行为类似数组,但它是由第一个类型索引的模板参数 std :: map - 因此在这种情况下,可以使用 std :: string 作为索引。



像这样:

  #include< map& 
#include< iostream>
#include< string>

struct StructName
{
int x;
};

std :: map< std :: string,StructName *> vars;

StructName a;
StructName b;

void registerVars()
{
vars [a] =& a;
vars [b] =& b;
}


int main()
{
std :: string v;

registerVars();

while(std :: cin>> v)
{
std :: cin.ignore(1000,'\\\
');

if(vars.find(v)== vars.end())
{
std :: cout< 没有这样的变量< std :: endl;
}
else
{
vars [v] - > x ++;
std :: cout<< variable<< v<< is<< vars [v] - > x< std :: endl;
}
}
return 0;
}


Good day,

I would like to reference a structure in a function by using a variable to store its name. Is this possible to do something like this in C++?

Definitely, all existing structures will be declared and initialised before any call is made (probably as global) and I will build in a check to make sure that only existing structures are referenced.

I would like something in this spirit:

struct StructName
{
...stuff
}a,b,c;

StructName a;
StructName b;
.
.
.

etc. including setting required values (in initialisation or elsewhere in code as needed)

and then I would have something like this to call from another portion of code:

void myFunction(char someInput)
{
some stuff
some stuff
externalFunction(static parameter, static parameter, _someInput_, static parameter);
yet some other stuff
}

where somInput is either a,b or c.

Please bear in mind I am a beginner with C, with little to no formal training in subject matter.

Thank you.

edit: If it was just myself, I would make do with case switch for someInput, referencing the structure directly in each case, but this part of a code is meant to be extendable by a non-programmer who would supply structures themselves, I would provide to him a template of structure initialisation code, and he would add the initialisation code to a specified place in the code, ammend the list of allowed names and compile the library.

解决方案

Variable names disappear as part of the compilation step(s) in C and C++.

Typically, there are two scenarios that solve the type of problem you are describing:

  1. User input corresponds to specific variable name.
  2. You don't actually want the "name" of the variable, but just need a way to associate different data with different parts of your code.

In the second, simpler case, just use an array, and use the index to the element you want as the way to associate the correct data.

In the first case, you can use a std::map<std::string, structname&>. A std::map acts sort of like an array, but it is indexed by the first type give in the template parameters to std::map - so in this case, you can use std::string as an index.

Something like this:

#include <map>
#include <iostream>
#include <string>

struct StructName
{
    int x;
};

std::map<std::string, StructName *> vars;

StructName a;
StructName b;

void registerVars()
{
    vars["a"] = &a;
    vars["b"] = &b;
}


int main()
{
    std::string v;

    registerVars();

    while(std::cin >> v)
    {
    std::cin.ignore(1000, '\n');

    if (vars.find(v) == vars.end())
    {
        std::cout << "No such variable" << std::endl;
    }
    else
    {
        vars[v]->x++;
        std::cout << "variable " << v << " is " << vars[v]->x << std::endl;
    }
    }
    return 0;
}

这篇关于如何通过名称存储在另一个变量(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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