Extending std::list

后端 未结 8 2107
小蘑菇
小蘑菇 2021-01-31 13:00

I need to use lists for my program and needed to decide if I use std::vector or std::list. The problem with vector is that there is no remove method and with list that there is

8条回答
  •  北海茫月
    2021-01-31 13:17

    Given your original problem statement,

    I need to use lists for my program and needed to decide if I use std::vector or std::list. The problem with vector is that there is no remove method and with list that there is no operator [].

    there is no need to create your own list class (this is not a wise design choice anyway, because std::list does not have a virtual destructor, which is a strong indication that it is not intended to be used as a base class).

    You can still achieve what you want using std::vector and the std::remove function. If v is a std::vector, then to remove the value value, you can simply write:

    #include 
    #include 
    T value = ...; // whatever
    v.erase(std::remove(v.begin(), v.end(), value), v.end());
    

提交回复
热议问题