文章目录
1. stack和queue的底层容器都是deque
注意:
由于stack的规则是先进后出, queue的规则是先进先出, 为了满足这种规则, 这两种容器都不提供iterator
, 也不允许遍历. 所以下面这种操作是错误的:
//stack和queue都不支持遍历, 不提供iterator
stack<string>::iterator ite; //has no member, error
queue<string>::iterator ite; // error
2. 替换底层容器可以吗?
答案是可以的, 但是只有部分容器可以替换
2. 1 用list替换
可以替换的, 因为list是双向链表, 支持pop_back, pop_front
等操作.
代码如下:
void test01(){
stack<string, list<string>> c;
char buf[10];
for(long i = 0; i < 10; ++i){
snprintf(buf, 10, "%d", rand());
c.push(string(buf));
}
cout << "stack.size() " << c.size(