0

I’m trying to implement my stl-like container, which in fact will be an incomplete version of a map. Faced a problem at the time of implementation of the erase method, I do not understand how to delete a pair of <key, value>. I am stuck, so I ask for help. Can someone please guide me to the solution? Here is the code. P.S.: Im only starting to learn C++;

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

                }
            }
    }

Whole code:

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;
};
Vardelosa
  • 3
  • 3
  • not the question, but you got some operators wrong, because you return `self_type` when it should be `self_type&`, eg `self_type operator++()` should be `self_type& operator++()` and you have pre/post the wrong way around. – 463035818_is_not_an_ai Apr 24 '20 at 13:59
  • see here: https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading/4421719#4421719 – 463035818_is_not_an_ai Apr 24 '20 at 13:59
  • Depends on what data-structure you're using to store `` pair in your `map`. In STL, `map` is typically implemented as a self-balancing binary search tree for which the `erase` key function takes `O(logn)` time. – srt1104 Apr 24 '20 at 14:05
  • Unrelated but erace is spelled: erase. – rustyx Apr 24 '20 at 14:23

1 Answers1

2

Since you represent the map as an array of pairs, this is the same as erasing an element from an array.

Since the order isn't important, you can swap the last element with the one you're removing, then destroy the removed element and adjust the size.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82