Question regarding sizing a very basic stack.

Nov 7, 2019 at 1:23am
Hi, so I have(hopefully), an extremely basic stack implementation. As far as functionality goes, it's useless. I'm just studying the proper creation. My code works, but I'm doing no sizing or resizing. I feel like this should be wrong since I'm using a vector(I think) to implement it.

Is there anything wrong with the following code?

Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <stack>
#include <list>

using namespace std;

int main(){

    stack<int> s;
    for (int i = 0; i < 10; i++){
        s.push(i);
    }
    
    while(!s.empty()){
        cout << "size is " << s.size() << endl;
        cout << "removing s. " << s.top() << endl;
        s.pop();
    }
    
}
Nov 7, 2019 at 1:31am
My code works, but I'm doing no sizing or resizing.
Yes you are. Elements are added to the top by calls to push, and elements are removed from the top with pop.

The possibility that a stack might internally be implemented by using a vector is inconsequential (ideally). All you should care about is the public interface of the stack, which are functions like push(), pop(), top(), etc.
Last edited on Nov 7, 2019 at 1:32am
Nov 7, 2019 at 1:39am
Understood.
So sizing happens automatically with push and pop.

Thanks!
Topic archived. No new replies allowed.