Erasing elements from a vector

后端 未结 5 808
萌比男神i
萌比男神i 2020-11-21 12:57

I want to clear a element from a vector using the erase method. But the problem here is that the element is not guaranteed to occur only once in the vector. It may be presen

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-21 14:00

    Depending on why you are doing this, using a std::set might be a better idea than std::vector.

    It allows each element to occur only once. If you add it multiple times, there will only be one instance to erase anyway. This will make the erase operation trivial. The erase operation will also have lower time complexity than on the vector, however, adding elements is slower on the set so it might not be much of an advantage.

    This of course won't work if you are interested in how many times an element has been added to your vector or the order the elements were added.

提交回复
热议问题