36項 copy_ifの正しい実装について理解しよう

copy_ifの正しい実装について理解しよう



STLには、copy_ifがありません(C++11でcopy_ifが標準に追加されました)
copy_ifを作成するには、下のようなコードを使います。

template<typename InputIterator,
     typename OutputIterator,
     typename Predicate>
OutputIterator copy_if(InputIterator begin,
            InputIterator end,
            OutputIterator destBegin,
            Predicate p )
{
    while(begin != end) {
         if (p(*begin)) *destBegin++ = *begin;
         begin++;
    }
    return destBegin;
}