本程序取自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/