HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Skip_over() algorithm

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
algorithmskip_overstackoverflow

Problem

I'm just curious if this is clear to the average person.

template
inline IteratorType skip_over(
    IteratorType begin,
    IteratorType end,
    typename std::iterator_traits::value_type skippedCharacter)
{
    typedef typename std::iterator_traits::value_type value_type;
    return std::find_if(begin, end, 
            std::not1(
                 std::bind2nd(std::equal_to(), skippedCharacter)
            )
        );
}

Solution

A few comments:

-
Algorithms typically use the names first and last for the iterators they take, not begin and end (in common usage, begin and end refer specifically to the iterators that delimit a range in a container).

-
The use of find_if seems a bit excessive: yes, it is good to use the Standard Library algorithms, but if you are writing your own algorithm, you may as well just write a loop, especially if it makes the code much clearer.

-
With respect to template parameter naming, it is helpful if you say what category of iterator is required; this helps to document the algorithm.

Consider the following, alternative implementation:

template 
ForwardIterator skip_over(ForwardIterator first, ForwardIterator last, T const& x)
{
    while (first != last && *first == x)
        ++first;

    return first;
}

Code Snippets

template <typename ForwardIterator, typename T>
ForwardIterator skip_over(ForwardIterator first, ForwardIterator last, T const& x)
{
    while (first != last && *first == x)
        ++first;

    return first;
}

Context

StackExchange Code Review Q#3830, answer score: 7

Revisions (0)

No revisions yet.