Erasing vector::end from vector

前端 未结 3 1402
谎友^
谎友^ 2020-12-10 00:45

Does it works correct(does nothing) when I use

 vector v;
 v.erase(v.end());

I want to use something like

 v.erase         


        
3条回答
  •  感情败类
    2020-12-10 01:15

    Erasing end() (or for that matter, even looking at the target of end()) 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 or unordered_set instead of vector here.

提交回复
热议问题