Home » C++ algorithm search_n() function

C++ algorithm search_n() function

by Online Tutorials Library

C++ Algorithm Functions search_n()

C++ Algorithm search_n() function searches the container [first,last) for the occurrence of a sequence of count elements, that is every element is searched to check whether it satisfies a given pred. An iterator to first element satifisying the condition is returned, else an iterator to last is returned.

Syntax

Parameter

first: It is a forward iterator to the first element of the range, where the element itself is included in the range.

last: It is a forward iterator to the last element of the range, where the element itself is not included in the range.

count: It gives the least number of elements that are supposed to be matching a condition.

val: The argument specifies the conditional value or the pred condition for which the function search_n is applied on a range.

pred: It is a Binary function that accepts two arguments, and gives a boolean result.

Return value

The function returns an iterator to the first element which matches the pred, if no such element is found then an iterator to the last element is returned.

Example 1

Output:

Two times 60 has been at position 2  

Example 2

Output:

Value u2 has been found at position 5  

Complexity

The complexity of the function is linear starting from the first element to the last one.

Data races

Some or all of the container objects are accesed.

Exceptions

The function throws an exception if any of the container elements throws one.

You may also like