Deleting elements from a vector

后端 未结 5 1098
独厮守ぢ
独厮守ぢ 2021-01-23 17:48

The following C++ code fills a vector with a number of objects and then removes some of these objects, but it looks like it deletes the wrong ones:

vector

        
5条回答
  •  不思量自难忘°
    2021-01-23 18:34

    the elegant way would be:

    std::vector photons = source->emitPhotons();
    photons.erase(
          std::remove_if(photons.begin(), photons.end(), isUseless),
          photons.end());
    

    and:

    bool isUseless(const Photon& photon) { /* whatever */ }
    

提交回复
热议问题