如何在c ++中实现复合容器类的迭代器

时间:2015-02-22 08:55:57

标签: c++

我正在实现一个容器类,它确保元素之间的唯一性,并将插入和删除限制为仅限于结尾。有点像一堆独特元素或带有push和pop等功能的有序集。它还必须具有固定的最大尺寸。

template <class T, int max_size>
class FixedSizedUniqueStack
{
    std::vector<T> m_vec;
    std::unordered_set<T> m_uset;
public:
    FixedSizedUniqueStack():m_vec(max_size),m_uset(){}
    bool push(T x)
    {
        bool success = true;
        if( m_uset.insert(x).second ) m_vec.push_back(x);
        else success = false;
        return success;
    }
    void pop()
    {
        if(m_vec.size() > 0)
        {
            m_uset.erase(m_vec.back());
            m_vec.pop_back();
        }
    }
    T back()
    {
        return m_vec.back();
    }
};

1 个答案:

答案 0 :(得分:0)

#include <vector>
#include <unordered_set>
#include <initializer_list>
template <class T, int max_size>
class FixedSizedUniqueStack:  public std::initializer_list<T>
{
protected:
    std::vector<T> m_vec;
    std::unordered_set<T> m_uset;
public:
    FixedSizedUniqueStack():m_vec(),m_uset(){}
    FixedSizedUniqueStack( const FixedSizedUniqueStack &x)
    {
        m_vec = x.m_vec;
        m_uset = x.m_uset;
    }
    FixedSizedUniqueStack& operator= ( const FixedSizedUniqueStack &x)
    {
        if (this != &x)
        {
            m_vec = x.m_vec;
            m_uset = x.m_uset;
        }
        return *this;
    }
    auto size() const -> decltype(m_vec.size())
    {
        return m_vec.size();
    }
    int push(const std::initializer_list<T>& il)
    {
        int errors = 0;
        for (auto x: il)
        {
            if( push(x) )
            {
                errors++;
            }
        }
        return errors;
    }
    int push(const T& x)
    {
        int error =0;
        if(m_vec.size() < max_size)
        {
            if( x < start)
            {
                error  = 1;
            }
            else if( x > stop)
            {
                error = 2;
            }
            else
            {
                if( m_uset.insert(x).second ) m_vec.push_back(x);
                else error = 3;
            }
        }
        else
        {
            error = 4;
        }
        return error;
    }
    void pop()
    {
        if(!m_vec.empty())
        {
            m_uset.erase(m_vec.back());
            m_vec.pop_back();
        }
    }
    T back()
    {
        return m_vec.back();
    }
    auto cbegin() const -> decltype(m_vec.cbegin())
    {
        return m_vec.cbegin();
    }
    auto cend() const -> decltype(m_vec.cend())
    {
        return m_vec.cend();
    }

    auto begin() const -> decltype(m_vec.begin())
    {
        return m_vec.begin();
    }
    auto end() const -> decltype(m_vec.end())
    {
        return m_vec.end();
    }

    auto empty() ->decltype(m_vec.empty())
    {
        return m_vec.empty();
    }