Search code examples
c++data-structuresstlstandards

Is there a way to access the underlying container of STL container adaptors?


Is there a standard way to access the underlying container of stack, queue, priority_queue ?

I found a method called : _Get_container() in VS2008 implementation of stack and queue, but no one for priority_queue! I think it is not standard anyway.

Also, I know it is a silly question! where can I find official documentation of the standard library ?


Just for clarification, I wasn't trying to mess up with the underlying container. All what I was trying to do is this :

template <class Container>
std::ostream& printOneValueContainer(std::ostream& outputstream, Container& container)
{
    Container::const_iterator beg = container.begin();

    outputstream << "[";

    while(beg != container.end())
    {
        outputstream << " " << *beg++;
    }

    outputstream << " ]";

    return outputstream;
}

// stack, queue
template
    < class Type
    , template<class Type, class Container = std::deque<Type> > class Adapter
    >
std::ostream& operator<<(std::ostream& outputstream, const Adapter<Type>& adapter)
{
    return printOneValueContainer(outputstream, adapter._Get_container());
}
.
.
.
std::stack<int> iStack;
.
.
std::cout << iStack << std::endl;

I hope you see that _Get_container() is not standard, and there is no one for priority_queue in VS2008 implementation.


Solution

  • I spotted the following solution somewhere on the web and I'm using it in my projects:

    template <class T, class S, class C>
        S& Container(priority_queue<T, S, C>& q) {
            struct HackedQueue : private priority_queue<T, S, C> {
                static S& Container(priority_queue<T, S, C>& q) {
                    return q.*&HackedQueue::c;
                }
            };
        return HackedQueue::Container(q);
    }
    
    int main()
    {
        priority_queue<SomeClass> pq;
        vector<SomeClass> &tasks = Container(pq);
        return 0;
    }
    

    Have fun :).