Does it works correct(does nothing) when I use
vector<T> v;
v.erase(v.end());
I want to use something like
v.erase(std::find(...));
Should I if
is it v.end()
or not?
There is no info about it on C++.com and CPPreference
Does it works correct(does nothing) when I use
vector<T> v;
v.erase(v.end());
I want to use something like
v.erase(std::find(...));
Should I if
is it v.end()
or not?
There is no info about it on C++.com and CPPreference
Have you tried this?
The standard doesn't quite spell it out, but
v.erase(q)
is defined, "Erases the element pointed to byq
" in[sequence.reqmts]
. This means thatq
must actually point to an element, which the end iterator doesn't. Passing in the end iterator is undefined behavior.Unfortunately, you need to write:
Of course, you could define:
c.erase()
on an associative container returnsvoid
, so to generalize this template to all containers you need some-> decltype
action.Erasing
end()
(or for that matter, even looking at the target ofend()
) is undefined behavior. Undefined behavior is allowed to have any behavior, including "just work" on your platform. That doesn't mean that you should be doing it; it's still undefined behavior, and I'll come bite you in the worst ways when you're least expecting it later on.Depending on what you're doing, you might want to consider
set
orunordered_set
instead ofvector
here.