-
Variable Name
Hello :)
Is it possible to do something like this:
Code:
int main()
{
//an array of variable names is made here
//(type?) name_array[2] //two names are prepared
int name_array[0]; //the first variable is declared as an integer and takes on the name of the first element in name_array
float name_array[1]; //the second variable is declared as a float and takes on the name of the second element in name_array
name_array[0] = 1; //values are
name_array[1] = 1.0; //assigned here
}
Something along the lines of an array containing prepared variable names distinguished solely by their index.
Thanks :)
-
You would have to declare an array of floats(correct me if im wrong)
-
Code:
struct Data
{
int m1;
float m2;
};
std::map<std::string, Data> Map;
Map["my_var_name"].m1 = 0!
Map["my_var_name"].m2 = 1.0f;
auto it = Map.find("my_var_name");
if (it != Map.end()) ; // Variable name is in map; otherwise not
See documentation for std::map.
-
The Bad news is what you are asking is impossible. The Good news you do not need it. :)
You can't name variables stored in an array. But just the name of the array is sufficient to access these variables. So, you don't need that feature.
If you really want to name the variables, you can create references to variables stored in an array.
What Elysia showed is storing variables specified in the logic of the program, which is not the same as the C++ variables.
-
But you can "map" strings to specific elements in an "array". Not directly, but this is essentially the same. You have a string, and you want to store specific data is identified by this string, For example.
This is what this is good for. But beware, the lookup time is O(logN), not O(1).