如何实现erase(key)map函数?

我正在尝试实现我的stl-like容器,它实际上将是一个不完整的地图版本.在实现擦除方法的时候面临一个问题,我不明白如何删除一对 <key, value>.我被卡住了,所以我请求帮助。谁能指导我解决这个问题?下面是代码.P.S.:我才开始学习C++。

void erace(const key_type& Key)
    {
            int n = 1;
            for (iterator i = begin(); i != end(); ++i, n++)
            {
                if (i->first == Key)
                {
                    //TO DO.

                }
            }
    }

整段代码.P.S.:我才开始学习C++;整段代码。

template < typename  Key, typename  T, typename  Compare = std::less<Key>, typename  Allocator = std::allocator< std::pair<const Key, T> > >
class map
{
public:
    class miterator
    {
        public:
            typedef miterator self_type;
            typedef std::pair<const Key, T> value_type;
            typedef std::pair<const Key, T>& reference;
            typedef std::pair<const Key, T>* pointer;
            typedef std::bidirectional_iterator_tag iterator_category;
            typedef std::ptrdiff_t difference_type;
            miterator(pointer ptr) : ptr_(ptr) { };
            miterator() {}
            self_type operator=(const self_type& other) { ptr_ = other.ptr_; return *this; }
            self_type operator++() { self_type i = *this; ptr_++; return i; }
            self_type operator--() { self_type i = *this; ptr_--; return i; }
            self_type operator++(int junk) { ptr_++; return *this; }
            self_type operator--(int junk) { ptr_--; return *this; }
            reference operator*() { return *ptr_; }
            pointer operator->() { return ptr_; }
            pointer operator&() { return ptr_; }
            bool operator==(const self_type& rhs) { return ptr_ == rhs.ptr_; }
            bool operator!=(const self_type& rhs) { return ptr_ != rhs.ptr_; }
        private:
            pointer ptr_;
    };
    class mconst_iterator
    {
    public:
        typedef mconst_iterator self_type;
        typedef std::pair<const Key, T> value_type;
        typedef std::pair<const Key, T>& reference;
        typedef std::pair<const Key, T>* pointer;
        typedef std::ptrdiff_t difference_type;
        typedef std::bidirectional_iterator_tag iterator_category;
        mconst_iterator(pointer ptr) : ptr_(ptr) { };
        mconst_iterator() {}
        self_type operator=(const self_type& other) { ptr_ = other.ptr_; return *this; }
        self_type operator++() { self_type i = *this; ptr_++; return i; }
        self_type operator--() { self_type i = *this; ptr_--; return i; }
        self_type operator++(int junk) { ptr_++; return *this; }
        self_type operator--(int junk) { ptr_--; return *this; }
        reference operator*() { return *ptr_; }
        pointer operator->() { return ptr_; }
        pointer operator&() { return ptr_; }
        bool operator==(const self_type& rhs) { return ptr_ == rhs.ptr_; }
        bool operator!=(const self_type& rhs) { return ptr_ != rhs.ptr_; }
        pointer ptr_;

    };

    typedef map<Key, T, Compare, Allocator> mymap;

    typedef Key key_type;
    typedef T mapped_type;
    typedef std::pair<const Key, T> value_type;
    typedef std::size_t size_type;
    typedef std::ptrdiff_t difference_type;
    typedef Compare key_compare;
    typedef Allocator allocator_type;
    typedef value_type& reference;
    typedef const value_type& const_reference;
    typedef value_type* pointer;
    typedef const value_type* const_pointer;
    typedef miterator iterator;
    typedef mconst_iterator const_iterator;
    typedef std::reverse_iterator<miterator> reverse_iterator;
    typedef std::reverse_iterator<mconst_iterator> const_reverse_iterator;





    map()
        : size(0), capacity(20), data(Allocator().allocate(20))
    {
    }

    map(const mymap& _Rhs)
        : size(_Rhs.size), capacity(_Rhs.size + 20), data(Allocator().allocate(_Rhs.size))
    {
        int count = 0;
        for (iterator i = &_Rhs.data[0]; i != &_Rhs.data[_Rhs.size]; ++i, ++count)
        {
            Allocator().construct(&data[count], *i);
        }
    }

    ~map()
    {
        if (!empty())
        {
            Allocator().deallocate(data, capacity);
        }
    }

    mymap& insert(const value_type& pair)
    {
        if (!is_present(pair))
        {
            if (++size >= capacity)
            {
                reserve(capacity * 2);
            }
            Allocator().construct(&data[size - 1], pair);
            return *this;
        }
    }

    bool is_present(const value_type& pair)
    {
        for (iterator i = begin(); i != end(); ++i)
        {
            if (i->first == pair.first)
            {
                return true;
            }
            return false;
        }
    }
    bool has_key(const key_type& _Key)
    {
        for (iterator i = begin(); i != end(); ++i)
        {
            if (i->first == _Key)
            {
                return true;
            }
        }
        return false;
    }

    mapped_type& operator[](const key_type& _Key)
    {
        if (has_key(_Key))
        {
            for (iterator i = begin(); i != end(); ++i)
            {
                if (i->first == _Key)
                {
                    return i->second;
                }
            }
        }
        size_type op = size;
        insert(value_type(_Key, mapped_type()));
        return data[op].second;
    }

    mymap& reserve(size_type _Capacity)
    {
        int count = 0;
        if (_Capacity < capacity)
        {
            return *this;
        }
        pointer buf = Allocator().allocate(_Capacity);
        for (iterator i = begin(); i != end(); ++i, ++count)
        {
            Allocator().construct(&buf[count], *i);
        }
        std::swap(data, buf);
        Allocator().deallocate(buf, capacity);
        capacity = _Capacity;
        return *this;
    }
    void erace(const key_type& Key)
    {
            int n = 1;
            for (iterator i = begin(); i != end(); ++i, n++)
            {
                if (i->first == Key)
                {
                   //TO DO

                }
            }
    }
    size_type get_size() const { return get_size; }

    bool empty() const
    {
        return size == 0;
    }
    iterator clear()
    {
        ~map();
    }
    iterator begin()
    {
        return &data[0];
    }
    iterator end()
    {
        return &data[size];
    }

    reverse_iterator rbegin()
    {
        return &data[0];
    }
    reverse_iterator rend()
    {
        return &data[size];
    }
    const_iterator cbegin() const
    {
        return &data[0];
    }
    const_iterator cend() const
    {
        return &data[size];
    }
    const_reverse_iterator rbegin() const
    {
        return &data[0];
    }
    const_reverse_iterator rend() const
    {
        return &data[size];
    }

    iterator find(const key_type& Key)
    {
        for (iterator i = begin(); i != end(); ++i)
        {
            if (i->first == Key)
            {
                return i;
            }
        }
        iterator res = end();
        return res;
    }
private:
    pointer data;
    size_type size, capacity;
};

解决方案:

由于你把地图表示为一个数组的对,这和从数组中擦除一个元素是一样的。

由于顺序并不重要,你可以把最后一个元素和你要删除的元素交换,然后销毁删除的元素,调整大小。

给TA打赏
共{{data.count}}人
人已打赏
未分类

python: 类函数返回值但不可用。

2022-9-9 6:47:21

未分类

为elasticsearch注册一个基于HDFS的快照仓库时,出现连接拒绝错误。

2022-9-9 6:58:16

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索