简体   繁体   English

std::stack 是否公开迭代器?

[英]Does std::stack expose iterators?

C++ STL 中的std::stack是否暴露了底层容器的任何迭代器,还是应该直接使用该容器?

Stack does not have iterators, by definition of stack.根据堆栈的定义,堆栈没有迭代器。 If you need stack with iterators, you'll need to implement it yourself on top of other container (std::list, std::vector, etc).如果您需要带有迭代器的堆栈,则需要在其他容器(std::list、std::vector 等)之上自己实现它。 Stack doc is here .堆栈文档在这里

PS According to a comment i got from Iraimbilanja, std::stack by default uses std::deque for implementation. PS 根据我从 Iraimbilanja 得到的评论,默认情况下 std::stack 使用 std::deque 来实现。

If you need a stack with iterators, you have two choices:如果您需要一个带有迭代器的堆栈,您有两种选择:

  • std::vector using push_back() , pop_back() . std::vector使用push_back()pop_back()

  • std::deque with either push_back() / pop_back() or push_front() / pop_front() . std::dequepush_back() / pop_back()push_front() / pop_front()

The std::stack does expose its underlying container (and therefore iterators) to subclasses through its protected interface. std::stack确实通过其受保护的接口将其底层容器(以及迭代器)暴露给子类 The std::stack 's underlying container object corresponds to the (protected) data member c . std::stack的底层容器对象对应于(受保护的)数据成员c So if you want to access them, you could extend std::stack a little.所以如果你想访问它们,你可以稍微扩展std::stack

template<typename T, typename Container = std::deque<T>>
class iterable_stack
: public std::stack<T, Container>
{
    using std::stack<T, Container>::c;

public:

    // expose just the iterators of the underlying container
    auto begin() { return std::begin(c); }
    auto end() { return std::end(c); }

    auto begin() const { return std::begin(c); }
    auto end() const { return std::end(c); }
};

int main()
{
    iterable_stack<int> st;

    st.push(2);
    st.push(5);
    st.push(3);
    st.push(7);
    st.push(9);

    for(auto i: st)
        std::cout << i << ' ';
    std::cout << '\n';
}

Output:输出:

2 5 3 7 9 

SGIMSDNGNU文档中, stack不提供迭代器。

Yor are asking你在问

Does std::stack expose iterators? std::stack 是否公开迭代器?

Many people gave answers.很多人给出了答案。 If my English would be better, I would maybe also understand the exact meaning of 'expose'.如果我的英语会更好,我也许也会理解“expose”的确切含义。

If we are referring to the STL and the class std::stack and the pre defined functions defined herein, the answer is NO.如果我们指的是 STL 和类 s​​td::stack 以及此处定义的预定义函数,答案是否定的。

My guess would be that you are asking, because you want to have iterators.我的猜测是你在问,因为你想要迭代器。

So, if we go one step further, we have the function top().所以,如果我们再进一步,我们就有了函数 top()。 And top() can be interpreted as a dereferenced iterator.而 top() 可以解释为一个解引用的迭代器。 With that, we can easily define Iterators to stack elements.有了这个,我们可以很容易地定义迭代器来堆叠元素。 The memory of the stack is guaranteed to be contiguous.栈的内存保证是连续的。

See below.见下文。 We are defining and using iterators for std::copy:我们正在为 std::copy 定义和使用迭代器:

#include <vector>
#include <stack>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <sstream>

using Number = int;
using UnderlyingContainer = std::vector<Number>;
using Stack = std::stack< Number, UnderlyingContainer>;

using StackIterator = const Number *;

std::istringstream testData("5 8 1 4 9 3");

int main()
{
    // Put the test data onto the stack
    Stack stack{ UnderlyingContainer {std::istream_iterator<Number>(testData),std::istream_iterator<Number>()} };

    // Print the test data
    // Get iterators
    StackIterator end = &stack.top() + 1;
    StackIterator begin = end - stack.size();

    if (not stack.empty())
        std::copy(begin, end, std::ostream_iterator<Number>(std::cout, "\n"));
    return 0;
}

So you can create iterators for a stack.因此,您可以为堆栈创建迭代器。 But, caveat:但是,警告:

The std::stack intentionally hides its elements under the hood. std::stack 有意将其元素隐藏在引擎盖下。 So, if you write-access the data, I would see it as a design fault.因此,如果您对数据进行写访问,我会将其视为设计错误。 Read-access through const pointers/iterators is for me OK.通过 const 指针/迭代器的读取访问对我来说是可以的。 But maybe you should better use a std::vector .但也许你应该更好地使用 std::vector 。 . . . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM