user3250183
user3250183

Reputation: 516

internal implementation of stl stack and queues

I am using a stl stacks and queues for storing a large collection of items. How is stack in standard template lib implemented internally? Is it in the form of linked list? or is there any maximum size given to it?

Upvotes: 7

Views: 6102

Answers (2)

vector works internally...


#include <iostream>
#include <vector>

struct Sample
{
    Sample(){}
    Sample(const Sample & obj)
    {
        std::cout<<"Sample copy constructor"<<std::endl;
    }
};
int main()
{
    std::vector<Sample> vecOfInts;

    std::cout<<"Capacity :: "<<vecOfInts.capacity()<<std::endl;
    std::cout<<"Size :: "<<vecOfInts.size()<<std::endl;
    int capcity = vecOfInts.capacity();
    for(int i = 0; i < capcity + 1; i++)
        vecOfInts.push_back(Sample());

    std::cout<<"Capacity :: "<<vecOfInts.capacity()<<std::endl;
        std::cout<<"Size :: "<<vecOfInts.size()<<std::endl;

    for(int i = 0; i < capcity + 1; i++)
            vecOfInts.push_back(Sample());

    std::cout<<"Capacity :: "<<vecOfInts.capacity()<<std::endl;
    std::cout<<"Size :: "<<vecOfInts.size()<<std::endl;

    return 0;
}

Upvotes: -3

Wojtek Surowka
Wojtek Surowka

Reputation: 20993

Both stacks and queues in C++ standard library are container adaptors. It means that they use specified container as the underlying means to store data. By default both of them use std::deque but you can use e.g. vector with

std::stack<int,std::vector<int>> s;

Upvotes: 9

Related Questions