简体   繁体   English

stl堆栈和队列的内部实现

[英]internal implementation of stl stack and queues

I am using a stl stacks and queues for storing a large collection of items. 我正在使用stl堆栈和队列来存储大量项目。 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? 或者是否有任何最大尺寸?

Both stacks and queues in C++ standard library are container adaptors . C ++标准库中的堆栈和队列都是容器适配器 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 eg vector with 默认情况下,它们都使用std::deque但你可以使用例如vector with

std::stack<int,std::vector<int>> s;
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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM