Ok so im trying to implement a stack using vectors in C++

i just want to know if the following implementation code makes sense, this is just the implementation file, the client file is working fine but when i run the program it doesnt work so i believe it has to do something wit the implementation file below. what the client program suppose to is let the user enter a postfix notation and the program will evaluate the result. Please any help

#include<iostream>
#include "stackV.h"
using namespace std;

stack::stack()
{
  maxsize = 100;
  currsize = 0;
}
                                                 
stack::~stack()
{
  clearIt();

}
                                                                          
bool stack::pop(el_t &)
{
    if(!isEmpty())
      {
        el.erase(el.begin() + 0);
        currsize--;
        return true;
      }
}

void stack::push(el_t n)
{
  if(!isFull())
    {
      el.insert(el.begin() + 0,n);
      currsize++;
    }

}
                                                                                                                                                                                                                  
el_t stack::topElem(el_t &n)
{
  if(!isEmpty())
    return el[0];

}                                                                                                                                                                                                                                                                                                                                                  
bool stack::isFull()
{
  return false;
}
                                                                                                                                                                                                                                                                                                                                         
bool stack::isEmpty()
{
  return(currsize == 0);
}                                                      
                           
void stack::displayAll()
{

  for(int i = 0; i < el.size(); i++)
    cout << el[i] << " " ;

}
  
void stack::clearIt()
{


}

Vector has push_back and pop_back methods, so just use the end of the vector as the top of the stack. You'll have to peek at the end to get the value before you pop it, though.

Stacks are available in the STL as container adapters. You can use a vector-based stack with the STL such as:

#include <stack>
stack<int, vector<int>>  myStack;

More details and a list of member functions can be found here:
http://www.cplusplus.com/reference/stl/stack/

im suppose to implement my own stack class using the vector class, and how would i implement peek and how would i apply it to pop?

std::vector has a size() method. Use it (and subtract 1) to find the index of the last element. Access that with [].

ok so i got this to access the last element, i dont know if its right and also how would i implement the pop function

el_t stack::topElem()
{
el_t n;

if(isEmpty())
cout <<"stack is empty";
else
{
n = el.back();
}
return n;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.