How to place variables within vectors?

Asked

Viewed 1,411 times

6

How do I create variables within a vector?

Something like:

int arr[] = {int x=1, int y = 2};
  • struct would serve you?

  • Please create an answer.

  • I gave an answer to start helping you. If you have specific questions about using struct and who knows later unordered_map, you will be asking. Reinforcement you should try to learn first how to do the basics of C++ which is the use of struct.

1 answer

8


You can’t do it the way you do it with Lua (that language you usually ask most here). C++ is a static language and has very different characteristics than you are used to.

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

Value map

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

Remember that? It works the same way only in C++ it doesn’t have the ease of syntax that a string was treated as if it were variable. You have to use only with syntax string. In the background Lua implements equal to this structure of C++ (of course in its way), but does exactly the same thing.

Example:

//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;
//acessar
cout << exemplo["x"] //imprime 1

This structure is available in the standard library starting with 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 very difficult to do right.

Note that in C++ everything needs to have a defined type. There are even ways to accept any type but you need to use a proper structure for this too.

C++, unlike Lua, has these things that give more work. In compensation it is much faster, powerful and safe. You can do in C++ everything you can do in Lua, but sometimes it takes more work.

Struct

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

Then you go like this:

//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 };
//acessar
cout << exemplo.x; //imprime 1

I put in the Github for future reference.

It’s a little more complicated than that but it’s a start for you to start studying the subject.

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

Which to use

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

  • I get it. I’m studying C/C++ exactly to better understand how things really work.

  • Thanks, when you edit like this, it’s faster than warning.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.