How to put variables within vectors?


How do I create variables within a vector?

Something like:

int arr[] = {int x=1, int y = 2};
asked by anonymous 17.12.2014 / 23:13

1 answer


You can not do this the way you do with Moon (which language you usually ask for more here). C ++ is a static language and has very different characteristics than you are accustomed to.

Or you create a struct or you create your own structure that maps values having some options.

Map of values

Probably to get something closer to what you get on Lua should use a ready-made standard library structure called unordered_map . With this structure you can create elements as you like as a string and store values in it.

Remember this ? It works the same way only in C ++ it does not have the ease of syntax that a string was treated as if it were variable. It has to be used only with string syntax. In the background, Moon implements this C ++ framework (of course, it does the same thing.)


//declara a variável do tipo unordered com chave do tipo string e valor do tipo int
std::unordered_map<std::string, int> exemplo;
exemplo["x"] = 1; //criou um elemento chamado "x" que vale 1
exemplo["y"] = 2;
cout << exemplo["x"] //imprime 1

This structure is available in the standard library from C ++ 11. In previous versions of the language you need to use an external library or make a structure like this on your own, which is quite difficult to do right.

Note that in C ++ everything must have type defined. You even have a way to accept any type but you need to use your own structure for this too.

C ++, unlike Moon, has these things that give more work. On the other hand, it is much faster, more powerful and safer. You can do in C ++ everything you can do in Lua, but sometimes it gives you more work.


But the most common thing in C ++ is to avoid this as far as it can and use something static, solved at compile time, in case it is best to use a struct .

There you go:

//declara a composição da estrutura para ser usada depois
struct estrutura {
    int x;
    int y;
//cria a variável exemplo do tipo estrutura e guarda os valores nos seus elementos
estrutura exemplo = { 1, 2 };
cout << exemplo.x; //imprime 1

I placed GitHub for future reference .

It's a bit more complicated than this, but it's a start for you to start studying.

This form is not exactly the same as the result you get with Lua but it is the most common one, which is what you try to do in the first place when programming in C ++. You should not look any other way until you really need it. Always look for the simplest and in C or C ++ a struct is what is called more idiomatic, closer to the language culture.

Which to use

Learn to use struct well. Even though it has some limitation, in the vast majority of cases it is it or its class variation that you should use. When you learn more and get to a point where you need more dynamism you will think of using a structure like unordered_map which has the advantage of dynamically putting in, taking and accessing elements (at runtime) but has poor performance for C ++ and can not check at compile time. This goes against the philosophy of C ++.

17.12.2014 / 23:30