Is this possible? I need to read the data from a stack (and a queue), but I need to maintain the stack (and queue) so they can be used later.

I have not found any useful information online. My only thought is loading the data into a parallel stack before I pop through the original stack while I write out the data.

Dani AI

Generated

A few practical options beyond "pop everything and lose it" — and why you saw mixed advice from and .

The simplest, portable approach is to copy the adapter and read from the copy. It preserves the original and uses only standard API (O(n) time and memory):

std::stack<T> s = /* ... */;
auto copy = s;                 // copy the whole stack
while (!copy.empty()) {
    process(copy.top());
    copy.pop();
}

If you need the exact pop order while iterating without modifying the original, copy-and-pop is clear and safe.

If you want iterator-style access, prefer one of two choices: use the underlying container directly (std::deque/std::vector) or expose the adapter's protected container by deriving a tiny wrapper that provides begin()/end() (this is standard C++ since the container is a protected member). Example for a read-only iterable stack:

template<class T, class Container = std::deque<T>>
struct iterable_stack : std::stack<T, Container> {
    using container_type = Container;
    using const_iterator = typename container_type::const_iterator;
    const_iterator begin() const { return this->c.begin(); }
    const_iterator end()   const { return this->c.end(); }

    using const_reverse_iterator = typename container_type::const_reverse_iterator;
    const_reverse_iterator rbegin() const { return this->c.rbegin(); } // top-to-bottom
    const_reverse_iterator rend()   const { return this->c.rend(); }
};

Notes and cautions: reading with begin() yields bottom-to-top; use rbegin()/rend() to match pop() order. Copying is simplest for assignments (as found). For production code, if you frequently need iteration, the cleanest design is to use the underlying container type directly or maintain a separate container that supports iteration — avoid repeatedly copying very large stacks.

Recommended Answers

All 4 Replies

You should be able to use iterators.

If you are using std::stack, then there isn't any concept of iterators for std::stack, which makes sense.

As for your question, you can copy the stack content if needed. But it looks like you shouldn't be using stack in the first place?

I am using std::stack. I ended up copying the stack content. It is for an assignment. I think the point is to make us aware that this is a problem.

Thanks for confirming I would need to copy!

I am using std::stack. I ended up copying the stack content. It is for an assignment. I think the point is to make us aware that this is a problem.

Thanks for confirming I would need to copy!

ok, cool beans then

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.