Doubt when defining vector in Pile

2

Let's imagine that we have the typical Pila class:

template <typename  tElemento> 
class Pila
{
  public:
.........
  private:
    tElemento *elementos;              //Vector de elementos
    int Lmax;                          //Tamaño del vector
    int tope_;                         //Posicion del tope
};

I can not understand how you are exactly defining the vector for a stack of eg numbers (1,2,3 ...), that is:

tElemento *elementos;

or what is the same:

int *elementos;

If in theory when we define a vector, for example, as if it were a stack, we define the vector in the following way:

int vector[Lmax] = {"1", "2", "3"...}; //Siendo Lmax constante definida

or

int vector2[] = {1,2,3,4,10,9,80,70,19};

Why does it really define the vector as a simple pointer within the class? I understand that a pointer contains the memory address that points to the first element of a vector, but does not define the vector in itself? It's what I can not understand.

Thanks

    
asked by ProgrammerJr 04.04.2018 в 18:55
source

1 answer

1

Context.

Normally the data containers do not save the raw data but a structure that wraps the data and describes the way in which one data is related to the next; this structure is usually called Node in many implementations.

For example, if you have Nodes that are related only to the next Node it would result in a list just linked , if instead it were linked to the next and previous node, it would be a list doubly linked , if you have a relationship higher-lower you could have a Node of binary tree or if the Node can contain multiple children it may be a < a href="https://en.wikipedia.org/wiki/%C3%81rbol_de_Merkle"> hash tree .

Batteries.

Stacks " stacked " data one on another and all operations are performed on the upper element; that type of behavior is easy to implement with a list simply linked, since you can point to the upper node and you will have data while the nodes have other nodes " below ".

Your class Pila has no nodes, so you have no way of describing how one data relates to the next, and therefore (as you have already observed) there is no way to easily manage the stack. But that does not mean it's impossible ...

Formations 1 .

  

Why does it really define the vector as a simple pointer within the class?

C ++ formations are a contiguous space in memory that store a type of data, the way to access them is through arithmetic of pointers . This is that by having a pointer to an element, you can add a value N to this pointer to access the Nth element from that pointer (or subtract N to access the Nth previous element); for this reason you can express a formation of arbitrary size with a simple pointer.

Your class stack.

We see that it has three members:

tElemento *elementos;              //Vector de elementos
int Lmax;                          //Tamaño del vector
int tope_;                         //Posicion del tope

Surely you expect to implement the stack by using a fixed-size array (stored in Lmax ) and remembering to which position elements are stored using the variable tope_ ; it is a type of implementation that can be more efficient than the implementation using Nodes as long as you have an initial training (vector elements) sufficiently large ... but if you exceed this initial capacity you should ask for memory again and re-host the above elements, which is usually very expensive.

  • Also known as arrays or English arrays.
  • answered by 04.04.2018 в 19:47