user2669764
user2669764

Reputation: 61

move values in stack to a vector in c++

I am trying to move the elements that are in the stack into a vector. I have tried the following:

for(int i=0; i<vector1.size();i++)
{
    vector1[i]=stack1.pop();
}

But it keeps giving me error.

Upvotes: 0

Views: 1987

Answers (3)

dspfnder
dspfnder

Reputation: 1123

That's because the pop() function does not return a value.

You need to use top() which returns the element at the top of the stack and then pop() to eliminate that element...

for (int i = 0; i<vector1.size(); i++)
{
    vector1[i] = stack1.top();
    stack1.pop();
}

Upvotes: 0

P0W
P0W

Reputation: 47854

std::stack::pop doesn't return a value

You need to use std::stack::top, to get top element and then remove it from the stack like following :

   vector1[i]=stack1.top();
   stack1.pop();

Upvotes: 4

slugonamission
slugonamission

Reputation: 9642

std::stack<T>::pop() doesn't return a value (it's a void function). You need to get the top, then pop, i.e.

for(int i=0; i<vector1.size();i++)
{
    vector1[i]=stack1.top();
    stack1.pop();
}

Upvotes: 0

Related Questions