코드
std::remove_if
by ehei
2021. 1. 21.
- <algorithm>
- 조건에 일치하는 반복자를 일치하는 않은 반복자 위치에 덮어씀. 따라서 조건을 만족한 컬렉션을 확인하면 중복된 데이터가 존재함
template<class ForwardIt, class UnaryPredicate>
ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p)
{
first = std::find_if(first, last, p);
if (first != last)
for(ForwardIt i = first; ++i != last; )
if (!p(*i))
// first 위치에 부합되지 않는 정보가 덮어씌워짐
*first++ = std::move(*i);
return first;
}
- 결과 값으로 검사를 끝낸 마지막 반복자가 나옴
- 따라서 반드시 정리가 필요
auto iter = std::remove_if(std::begin(c), std::end(c), cond);
c.erase(iter, std::end(c));
- map, unordered_map을 위해 erase_if가 별도로 존재