简体   繁体   中英

std::vector::erase with equal arguments

Is it safe to call iterative methods such as STL container erase (the override taking 2 iterators), eg in my case std::vector::erase() , with both arguments being vec.end() ? I made an experiment and it looks like the call just does nothing, but I want to be sure I can use it safely in the code, otherwise I have to test my iterators and avoid erase() if they're equal.

C++ Standard (n3337), 23.2.3, Sequence containers, Table 100 ('sequence container requirements) - row a.erase(q1,q2) :

Effects: Erases the elements in the range [q1,q2) .

This means that a.erase(q,q) is safe because it does nothing.

Loki Astari's answer to this SO question has a list of drafts of the C++ standard. The official copy is not freely available unfortunately, so I am using n3337, which seems to be the closest to C++11.

It is safe ; it is an effective no-op because end() -> end() is a range that is empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM