copy_if

Tags:

algorithm copy_if implementation after Scott Myers

[copy_if.hpp]

#ifndef __copy_if
#define __copy_if
	
template<typename input_iterator,
         typename output_iterator,
         typename predicate>
output_iterator copy_if(input_iterator begin,
                        input_iterator end,
                        output_iterator dest_begin,
                        predicate p);
	
#endif

[copy_if.cpp]

#include "copy_if.hpp"
	
template<typename input_iterator,
         typename output_iterator,
         typename predicate>
output_iterator copy_if(input_iterator begin,
                        input_iterator end,
                        output_iterator dest_begin,
                        predicate p)
{
    while (begin != end)
    {
        if (p(*begin)) *dest_begin++ = *begin;
        begin++;
    }
	
    return dest_begin;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *