Does the final iterator erase the supervision in a standard or design solution?

Standard library containers allow us the eraseranges indicated by firstand iterators last.

std::vector<foo> bar;
//         first it     last it
bar.erase(bar.begin(), bar.end());

The standard states that an iterator firstmust be valid and dereferenced, while it lastmust be valid. However, if first == last, then there is firstno need to play, because it eraseis no-op. This means that it is legal:

bar.erase(bar.end(), bar.end());

However, if I want to remove only one element, not a range, then the iterator must be valid and dereferenced, creating the following undefined behavior:

bar.erase(bar.end());

-? , , ?

, , - :

bar.erase(std::find(bar.begin(), bar.end(), thing));
+4
2

++ - , " , ". . , , , , .

erase(iterator it):
    if it != end: // we can't avoid this check now
        // erase element

, , . :

erase(iterator first, iterator last):
    while first != last:
        erase(first++);

, . , memmove , .


, , :

checked_erase(Container cont, iterator it):
    if it != cont.end():
        cont.erase(it);
+3

-?

no-op? , , , - . . , , undefined, - , , , .

: , , std::remove, std::find. , , .

-: erase(it) erase(it, it+1) . . , erase(it) , erase(it, it+1) undefined, .

+1

All Articles