c++ - 以两种数据类型作为参数的堆栈 STL

标签 c++ stack

本程序取自cplusplus.com

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

int main ()
{
    deque<int> mydeque (3,100);     // deque with 3 elements
    vector<int> myvector (2,200);   // vector with 2 elements

    stack<int> first;               // empty stack
    stack<int> second (mydeque);    // stack initialized to copy of deque

    stack<int,vector<int> > third;  // empty stack using vector
    stack<int,vector<int> > fourth (myvector);

    cout << "size of first: " << (int) first.size() << endl;
    cout << "size of second: " << (int) second.size() << endl;
    cout << "size of third: " << (int) third.size() << endl;
    cout << "size of fourth: " << (int) fourth.size() << endl;

    return 0;
}

我不明白的是,我们为什么要提到 stack<int, vector<int>>即两种数据类型而不仅仅是 stack<vector<int>>

最佳答案

checkout :http://www.sgi.com/tech/stl/stack.html

为模板创建一个具有两个数据类型参数的堆栈 stack<T, Sequence> stack;

这样做是因为第一个类型参数是栈中元素的类型,第二个类型参数是用来实现栈的容器类型。

使用不同的容器类型会为您提供不同的内存分配、速度等方面的优缺点。它只是让您作为消费者在希望使用的实现类型方面具有更大的灵 active 。

来自该链接:

Stack is a container adaptor, meaning that it is implemented on top of some underlying container type. By default that underlying type is deque, but a different type may be selected explicitly.

关于c++ - 以两种数据类型作为参数的堆栈 STL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12729732/

相关文章:

c++ - 对 int 类型和无符号整数的值应用模运算

c - 如何在C编程中使用链表将堆栈链接到其他堆栈?

c - 为什么访问数组元素时只访问一次内存

java - BST 的迭代方法

c++ - 如何在 C++ 中打印 double

c++ - MS Visual C++ : When should you care about using calling conventions?

使用列内的固定效果信息 reshape R 中的数据

delphi - 有趣的堆栈问题?

c++ - isdigit 在输入 £ 和 ¬ 时引发调试断言

c++ - 向函数添加具有默认值的参数是否会破坏 ABI?