[在所有类型的迭代器中,为什么stack
,queue
和priority_queue
STL容器没有受支持的模式?
#include <iostream>
#include <stack>
#include <algorithm>
int main(){
std::stack<int> values;
std::stack<int> valuesCopy;
values.push(98);
values.push(11);
values.push(14);
values.push(17);
values.push(20);
std::for_each( /* How can i manage this in a alternative way */,
[&](int value) mutable throw() -> void{ /* Process */ ;} );
std::copy(/* Same for this */,std::back_inserter(valuesCopy));
return 0;
}
这不是经典容器,而是container adaptors。他们不需要支持迭代。引用“ C ++编程语言”这本书:
容器适配器提供对底层的专门访问容器。
它们是:
旨在仅通过其专用接口使用。特别是STL容器适配器不提供直接访问其基础容器的权限。它们不提供迭代器或下标。