ok so im trying to implement a stack using vector and i have to access the last element of the vector el, i dont know if its right and also how would i implement the pop function of the stack using vector

el_t stack::topElem()
{
el_t n;

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

Are you required to use vector to implement a stack, or do you just need a stack? In the latter case,

#include <stack>

works fine.

If you need to implement a stack with vector, though, this may work:

#include <vector>

template <class T> // you can skip the templates if you have a specific type in mind
class mystack
{
   public:
      // put your public functions here
   private:
      std::vector<T> data;
};

Consider how you would adapt the vector member functions, especially

void push_back (T);
T back ();
void pop_back();

to perform stack-related functions.

Edited 5 Years Ago by fishsicles: Old version gave away the answer a little too easily.

**Please use code tags**

I don't really understand your question. The vector class in the STL is already an implementation of a stack (in fact almost all basic STL containers are). With either vector or list:
- to push an element on the stack, you use push_back()
- to pop an element off the stack, you use pop_back()
- to access the element on top of the stack, you use back()
That's all there is to it. If you want more "stack-like" vocabulary, you can use the stack class of the STL. This just wraps an STL container with the above functions into a class that has functions that are characteristic of a stack. So, to use vector for a stack, you do this:

#include <vector>
#include <stack>
#include <iostream>
using namespace std;

int main() {
  stack< int, vector<int> > my_stack; //now, this is a stack stored in a vector.
  my_stack.push(42);
  cout << my_stack.top() << endl;
  my_stack.pop();
  if(my_stack.empty())
    cout << "the stack is now empty." << endl;
  return 0;
};

I don't know why you would want to make your own stack class, but if you still do. Then make question more precise and read the STL documentation well, it usually contains all the answers and examples needed.

yeah it is homework and so i have this so far for push and pop and i want to make sure its fine
my vecor is called vector<el_t> el;

void stack::pop(el_t & n)
{
  if(isEmpty())
    cout <<"stack is empty";
  else
    n = el.back(); //Accesing the last element                                              
  el.pop_back();   //Removing the last element                                              


}
void stack::push(el_t n)
{
  if(!isEmpty())
    {
      el.push_back(n);
    }

}

I think that you meant to enclose lines 6 and 7 into a { } brackets (because as is, the pop_back function will be called even though the vector is empty).

As for the push function, I don't get it. Why is it necessary for the vector to be non-empty? How would you fill the stack in the first place if you forbid the pushing of an element when it's empty. I think that if-statement should just disappear.

This article has been dead for over six months. Start a new discussion instead.