Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #736

Destructive erase for associative containers

Message-ID <5d9c5e92-7351-430f-8e1a-4c0f25d7b9c8@googlegroups.com> (permalink)
Newsgroups comp.std.c++
From travis@gockelhut.com
Subject Destructive erase for associative containers
Organization unknown
Date 2014-10-10 14:51 -0600

Show all headers | View raw


There are a number of use cases where I want to entirely consume the
contents of an associative container and re-use the memory of the keys. If
I have an expensive to copy yet cheap to move key type, there is not a
supported way of moving my key to a new object, even though I am about to
erase said key from the container. I would love an operation that allowed
me to extract and re-use the key when I am moving it to another structure.
All associative containers could benefit from such an operation. The need
for this operation isn't terrifically common, but I think it is generic
enough to warrant a discussion.

In an std::map<Key, Value>, you might have member functions like so:

    std::pair<iterator, std::optional<std::pair<Key, Value>>>
    destroy(const Key& position);

    std::pair<iterator, std::pair<Key, Value>>
    destroy(const_iterator position);

For std::set<Key>:

    std::pair<iterator, std::optional<Key>>
    destroy(const Key& position);

    std::pair<iterator, Key>
    destroy(const_iterator position);

Forgive the poor naming...I can't think of a better one. I also can't
figure out a great way to make this scale to multimap/multiset.

The concept will feel familiar if you know Rust's container removal
operations (things like std::Vec<T>::pop return an Option<T> with the value
you popped moved into the Option).


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextNext in thread | Find similar


Thread

Destructive erase for associative containers travis@gockelhut.com - 2014-10-10 14:51 -0600
  Re: Destructive erase for associative containers Öö Tiib <ootiib@hot.ee> - 2014-10-15 13:27 -0600

csiph-web