Home » C++ algorithm search() function

C++ algorithm search() function

by Online Tutorials Library

C++ Algorithm Function search()

C++ Algorithm search() function searches the range [first1, last1) for the occurrence of a subsequence defined by the range [first2, last2), and an iterator to the first element is returned. If the subsequence does not exist then an iterator to the last1 is returned.

Syntax

Parameter

first1: It is a forward iterator to the first element of the [first1, last1).

last1: It is a forward iterator to the last element of the [first1, last1).

first2: It is a forward iterator to the first element of the [first2, last2).

pred: It is a binary function that accepts two elements as arguments and performs the task designed by the function.

Return value

The function returns an iterator to the first element of the first occurrence of the subsequence, else returns the last1 element.

Example 1

Output:

patt1 found at position 1  patt2 not found  

Example 2

Output:

Vector2 is present at index:2  

Complexity

The function has linear complexity from the first1 element to the last1 element.

Data races

Objects in both ranges are accessed.

Exceptions

The function throws an exception if any of the argument throws one.

You may also like