Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between std::search and std::find_first_of

Tags:

c++

find

std

I am trying to grasp the difference between std::search and std::find_first_of

They have the same prototypes:

template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
                                   ForwardIterator2 first2, ForwardIterator2 last2);

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
                                   ForwardIterator2 first2, ForwardIterator2 last2,
                                   BinaryPredicate pred);

template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
                            ForwardIterator2 first2, ForwardIterator2 last2);

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
                            ForwardIterator2 first2, ForwardIterator2 last2,
                            BinaryPredicate pred);

They both return the same thing: An iterator to the first occurrence of the sequence [first2, last2) inside [first1, last1). (using equality, or a binary predicate)

So what is the difference ? Am I wrong ?

like image 620
Stephane Rolland Avatar asked Jan 09 '14 10:01

Stephane Rolland


1 Answers

The difference is that std::search searches for a whole range of elements within another range, while std::find_first_of searches for a single element from a range within another range.

like image 160
juanchopanza Avatar answered Sep 17 '22 16:09

juanchopanza