C ++: saving structures on the stack

I have a structure:

    struct Vehicle
{
    char ad; // Arrival departure char
    string license; // license value
    int arrival; // arrival in military time
};

I want to keep all the values ​​in the structure on the stack.

I can save one value on the stack by doing:

    stack<string> stack; // STL Stack object
    Vehicle v; //vehicle struct object
    stack.push(v.license);

How can I save the entire structure on the stack in order to subsequently access char, int and string?

+5
source share
4 answers

Simple, just replace stringwith Vehicleand instance stringfor instance Vehicle:

stack< Vehicle > stack; // STL Stack object
Vehicle v; //vehicle struct object
stack.push(v);
+10
source

The type between <and >is what your stack will hold. The first one contained strings, you can have Vehicles:

std::stack<Vehicle> stack;
Vehicle v;
stack.push(v);
+6
source

, v ? , :

void Foo(Stack <Vehicle>& stack) {
  Vehicle* vPtr = new Vehicle();
  stack.push(vPtr);
}
+1

g ?

#include<iostream>
#include<stack>
using namespace std;
struct node{
    int data;
    struct node *link;
};

main(){
    stack<node> s;
    struct node *g;
    g = new node;
    s.push(g); 
}
0

All Articles