Help with vectors inisialitation

Write your question here.
Hello everybody , well i have a question about vectors , i used to use them normally with matrices and all of that but in the code here below i don't understand the use of auto and where do i get kase? , I mean where is the declaration of this variable or it's going to exist only in the two cycles for? , by the other hand ,the beginning of code , the use of typedef i don't get it , could anybody give some clues , her i'll let the code

typedef < array<array<ColorCasilla , 3> , 3> Celda ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  Put the code you need help with here.
Enum ColorCsilla{VACIO, CIRCULO,CRUZ};
typedef < array<array<ColorCasilla , 3> , 3> Celda ;


class juego{
public:

void inisalizar(){

for (auto& linea :celda){
for(auto& kase : celda){
    kase = VACIO;
			}
			}

}
private:

Celda celda; 
}
Here is a quick breakdown:

Celda is now a type. Think of it as replacing Celda with array<array<ColorCassila, 3>, 3>. Hence line 20 becomes array<array<ColorCassila, 3>, 3> celda;.

auto is a keyword to tell the compiler to work out the type of the variable itself, from the information given to it. Here are some examples:
1
2
3
auto x = 5; // x is an int
auto y = "Hello!"; // x is a char*
auto z = celda.begin(); // z is an array<array<ColorCasilla, 3>>::iterator 

In your code, it simply retrieves the data type of the celda object, like this:
1
2
// for (auto& linea : celda)
for (array<ColorCasilla, 3>& linea : celda)


Ranged based for loops simply take a container of some kind, and loop through that container setting the variable to the contents of that container's iteration. For example, that loop is the same as:
 
for (size_t i = 0, auto& linea = celda[0]; i < celda.size(); ++i)


As a side note, that loop is wrong anyway, you probably wanted this:
1
2
3
4
5
6
7
void inisalizar() {
    for (auto& linea : celda) {
        for (auto& kase : linea) {
            kase = ColorCasilla::VACIO;
        }
    }
}


If you don't understand, try looking up some of the things I mentioned, like range based for loops or the like.
I assume you want to set each element of celda to VACIO.

First I need to improve your typedef.
1
2
typedef array<ColorCasilla, 3> Linea;
typedef array<Linea, 3> Celda;


The explicit code to do this is:
1
2
3
4
5
6
7
8
9
10
11
for (Celda::iterator p = celda.begin(); p != celda.end(); ++p)
{
    Linea& linea = *p;

    for (Linea::iterator q = linea.begin(); q != linea.end(); ++q)
    {
        ColorCsilla& estilo = *q;

        estilo = VACIO;
    }
}


That can be expressed more concisely with:
1
2
3
for (auto& linea : celda)
    for (auto& estilo : linea)
        estilo = VACIO;
Last edited on
Topic archived. No new replies allowed.