1
Manjia
2d

Everybody knows that removing an element from a vector while iterating is a complex task for almost any language -_-' but just try to do it with c++ and get ready to discover that the most robust language is also the one that sucks shit the most

Comments
  • 1
    How big of a vector are we talking? The FP approach would just copy the vector using copy_if to a new vector. Is it a vector for performance reasons?
  • 4
    Iterators aren't really meant for this use case, you want a filter operation or something
  • 2
    The C++ standard library is pretty clear about its guarantees. If you'd like to remove an element (or even insert an element in the general case) and preserve iterators/pointers to other elements, std::vector is not the right data structure.
  • 2
    "most robust language" - citation needed
  • 2
    Sounds like an accidental O(n^2) in the making.
  • 2
    linked lists make that absolutely trivial?
  • 3
    You should not use a 15 kg hammerhead to make a nail...

    An iterator iterates. That's it.

    While some languages implement an eg "copy on write" approach to allow modification... It's in my opinion definitely wrong - yes, yadda yadda easy and comfortable, but most approaches (as other people mentioned) will create an copy per modification...

    Oops.

    There are many alternatives...

    Stream -/ Map & Filter
    Loop -/ Index & Modify
    ...

    An iterator is a simplified way to allow a generalized, read only access to any data structure - object / array / list, you name it.
  • 0
    i call for @FrodoSwaggins for some input
  • 1
    why the fuck would you do that?
  • 2
    @Root But linked lists are usually a very bad data structure these days because they trash cache locality.
  • 2
    @Fast-Nop Plus they're space inefficient for small elements, which also is terrible for cache.
Add Comment