본문 바로가기
코드

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가 별도로 존재

'코드' 카테고리의 다른 글

make_index_sequence 구현  (0) 2021.01.27
tuple 출력  (0) 2021.01.22
instance in underlying array  (0) 2021.01.21
forward declaration of unique_ptr<T>  (0) 2021.01.21
최신 STL 배우는 방법  (0) 2020.12.15