c++ - Efficient way to remove elements from std vector according to predicate -
i'm writing algorithm supposed remove set of points stored inside vector every element inside of list of rect supply.
i'm using testing ground c++11 so, since i'm still getting used new features, know if efficient approach or if has particular flaw i'm not getting.
vector<tuple<u16, u16, u16, u16>> limits; for_each_area_to_remove limits.push_back(make_tuple( area->x - viewport_size_x/2, area->x + viewport_size_x/2, area->y - viewport_size_y/2, area->y + viewport_size_y/2)); for_each_area_to_remove_end vector<point2d> points; remove_copy_if(suitablepoints.begin(), suitablepoints.end(), points.begin(), [&](const point2d &point) { (auto limit : limits) if (point->x > get<0>(limit) && point->x < get<1>(limit) && point->y > get<2>(limit) && point->y < get<3>(limit)) return true; return false; } );
this seems more trivial solution problem, create vector of bounds must excluded point set , iterate on set point. wonder if there's better approach problem. point out set of points huge, while set of rects indeed enough limited.
you change auto
auto const&
, since not need create copy of each rectangle in limits
iterate through collection:
for (auto const& limit : limits) // ^^^^^^
this should bring performance improvement (but when performance concerned, measure before drawing conclusions).
also, unless need create copy of elements remove vector (the text of question not mention this), use std::remove_if()
instead of std::remove_copy_if()
.
std::remove_if()
works overwriting removed elements subsequent ones, , return new logical end of vector without resizing vector (which desirable behavior if not need that).
it therefore whether or not calling std::vector::erase()
after std::remove_if()
. a common practice has name.
Comments
Post a Comment