rekkalmd
rekkalmd

Reputation: 181

Why there is no supported iterators for some STL containers (Stack, Queue, Priority Queue)?

Across all types of iterators, why there is no supported pattern for stack, queue and priority_queue STL containers ?

#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;
}

Upvotes: 0

Views: 293

Answers (1)

Ron
Ron

Reputation: 15511

The three are not classic containers, but rather container adaptors. They do not need to support the iteration. A quote from the "C++ Programming Language" book:

Container adaptors provide specialized access to underlying containers.

They are:

intended to be used only through their specialized interfaces. In particular, the STL container adaptors do not offer direct access to their underlying container. They do not offer iterators or subscripting.

Upvotes: 6

Related Questions