简体   繁体   English

我如何在 c++ 中实现我自己的堆栈迭代器

[英]how can i implement my own stack iterator in c++

i've been trying to implement an iterator to my stack like this:我一直在尝试对我的堆栈实现一个迭代器,如下所示:

#include <iostream>
#include <stack>
#include <deque>

template <typename T, class container=std::deque<T>>
class MutantStack : public std::stack
{
    public:
        MutantStack(){}
        ~MutantStack(){}
        MutantStack(const MutantStack &stack)
        {
            *this = stack;
        }

        typedef typename std::deque::iterator iterator;
};

but i couldn't make a begin and end iterator, how i can do it?但我无法制作开始和结束迭代器,我该怎么做? and another question what the c.begin() in deque iterator means, i found this exemple:另一个问题是双端队列迭代器中的 c.begin() 是什么意思,我发现了这个例子:

iterator begin()
{
return this->c.begin();
}

I am not sure, if you selected the correct approach.我不确定,如果您选择了正确的方法。

Because, you can use a std::deque in the first place.因为,您首先可以使用std::deque It offers all functionality that a stack offers.它提供了堆栈提供的所有功能。 And more.和更多。

So maybe better use a std::deque or a std::vector .所以也许更好地使用std::dequestd::vector

Additionally, deriving from standard containers is not the best idea.此外,从标准容器派生并不是最好的主意。 You may read about that here on SO.您可以在 SO 上阅读有关此内容的信息。

But if you want to do that for exceptional purposes, then simply take the address of the top() .但是,如果您想出于特殊目的这样做,那么只需获取top()的地址。 This will be the last element in the underlying container.这将是底层容器中的最后一个元素。 And if you subtract the size() of the stack (corrected by 1), then you have a pointer to the beginning of the underlying container.如果你减去堆栈的size() (修正 1),那么你就有一个指向底层容器开头的指针。

And then you can use the artificial iterator and subscript operator [] as expected.然后您可以按预期使用人工迭代器和下标运算符[]

Please see the following example:请看下面的例子:

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

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

using StackIterator = Number *const;

int main()
{
    // Put the test data onto the stack
    Stack myStack{ UnderlyingContainer {1,2,3,4,5} };

    if (not myStack.empty()) {

        // Get "iterators"
        StackIterator end = &myStack.top() + 1;
        StackIterator begin = end - myStack.size();

        Number *const & stk = begin;

        for (size_t i{}; i < myStack.size(); ++i)
            stk[i] = stk[i] + 10;

        for (size_t i{}; i < myStack.size(); ++i)
            std::cout << stk[i] << '\n';

        std::transform(begin, end, begin, [](const Number n) {return n - 10; });
        std::copy(begin, end, std::ostream_iterator<Number>(std::cout, "\n"));
    }
}

So, it looks like we found what you want to have, but in reality, we simply work on the underlying container.所以,看起来我们找到了你想要的东西,但实际上,我们只是在底层容器上工作。

after many research i found this solution:经过多次研究,我找到了这个解决方案:

template <typename T, class container=std::deque<T>>
class MutantStack : public std::stack<T>
{
    public:
        MutantStack(){}
        ~MutantStack(){}
        MutantStack(const MutantStack &stack)
        {
            *this = stack;
        }
        typedef typename container::iterator iterator;
        iterator begin()
        {
            return this->c.begin();
        }
        iterator end()
        {
            return this->c.end();
        }
};

i found out that stack is just a child inheret from the deque type like this:我发现 stack 只是 deque 类型的一个孩子,如下所示:

template <class Type, class Container = deque<Type> > class stack;

but taking only a few methods like: pop push empty swap and emplace.但只采用几种方法,例如:pop push empty swap 和 emplace。 so it has also the deque iterator as well, so i used it as above.所以它也有双端队列迭代器,所以我像上面一样使用它。 the "c" in c.begin() and c.end() is a container_type defined in the stack class: c.begin() 和 c.end() 中的“c”是在堆栈 class 中定义的容器类型:

public:
    typedef _Container                               container_type;
protected:
    container_type c;

means c is the container stored data and when typing c.begin() we actually heading to the first value in Mutatnstack it's like value[0] in array.表示 c 是容器存储的数据,当键入 c.begin() 时,我们实际上是指向 Mutatnstack 中的第一个值,它就像数组中的 value[0]。 Now my MutantStack class inheret from Stack class that's her self inheret from deque class:现在我的 MutantStack class 从堆栈 class 继承,这是她从双端队列 class 继承:

     MutantStack -> Stack -> deque
                  which is the same as :
     MutantStack -> deque

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

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