If I had some array numbers[] = [1, 2, 3, 4]
and array[] = ["numbers", "letters", "characters"]
, would it be possible to call the array numbers
with the name "numbers"
in C++? I don't want to call and individual element but use the name in one array to call a separate array.

- 633
- 4
- 15

- 13
- 3
-
3Use a map of some sort – StoryTeller - Unslander Monica Jan 29 '18 at 06:41
-
1@gj13 That seems to me to require more explanation. Consider making an answer to have more features of formatting and space at your disposal. – Yunnosch Jan 29 '18 at 06:46
-
Are numbers, letters and characters arrays of type __int__ or arrays of different types? You can have pointers to arrays of the same type or arrays of pointers to derived types but not arrays of different types. – cup Jan 29 '18 at 07:04
-
3[XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – n. m. could be an AI Jan 29 '18 at 07:07
-
@cup in the context I use it, the called arrays are always **ints**, I just picked random names as an example – UnknownM1 Jan 29 '18 at 07:07
-
This technique is called reflection. Possible duplicate of [Convert string to variable name or variable type](https://stackoverflow.com/questions/7143120/convert-string-to-variable-name-or-variable-type) – xskxzr Jan 29 '18 at 07:11
-
@UnknownM1, this starts with weak architecture .. and ends up in messy code. – Mohammad Kanan Jan 29 '18 at 07:25
2 Answers
When people have some questions like this, it means that people start trying to create the script languages... Keep focusing on this question will make you create a script language whose name might be PHP, bash...
You are touching the philosophy of programming: how does a language see the world. If you can speak another language except your native language, you will get such a truth: if you speak a different language, your world will be different. For programming languages, it's the same. The way how the C++ sees the world doesn't support the dynamic variable name, which is actually you are asking.
Read these:
https://coderwall.com/p/al0aqq/dynamic-variable-name-in-bash
https://unix.stackexchange.com/questions/60584/how-to-use-a-variable-as-part-of-an-array-name
http://php.net/manual/en/language.variables.variable.php
For your need, I suggest using std::map<std::string, std::vector<int>
. Or if necessary, use template: std::map<std::string, T>
.
As @Nybbit said, you can use std::unordered_map
too. It depends on your need. Google the difference between std::map
and std::unordered_map
.

- 11,597
- 17
- 83
- 180
-
1If you don't need the elements to be ordered, I'd recommend looking into [unordered_map](http://en.cppreference.com/w/cpp/container/unordered_map) – Nybbit Jan 29 '18 at 06:58
You can refer to the array by name.
int numbers[] = {48, 49, 50, 51};
int letters[] = {65, 66, 67, 68};
int characters[] = { 31, 32, 33, 34};
int* array[] = {numbers, letters, characters};
Note that this takes the address of the array: not the string name. To make life easier, you might want to use enums
enum {ix_number, ix_letter, ix_char};
...
std::cout << array[ix_letter][0] << std::endl;
...
// alternatively
int* group = array[ix_char];
std::cout << group[2] << std::endl;

- 7,589
- 4
- 19
- 42