A simple program interprets the C ++ STL algorithm series 3: find_if

Source: Internet
Author: User

 

C ++ STL's Non-mutating algorithms is a set of template functions that do not destroy operation data, it is used for processing sequence data one by one, element search, subsequence search, statistics, and matching.

 

The find_if algorithm is a predicate version of the find algorithm. It uses the predicates that return boolean values to determine the pred and checks every element in the iterator range [first, last, if the iterator iter satisfies pred (* iter) = true, the element is found and the iterator value iter is returned. If no element is found, the last is returned.

 

Function prototype:

 

 

Template <class InputIterator, class Predicate>

InputIterator find_if (

InputIterator _ First,

InputIterator _ Last,

Predicate _ Pred

);

 

Sample Code:

 

 

/*************************************** ****************************

* Copyright (C) Jerry Jiang

* File Name: find_if.cpp

* Author: Jerry Jiang

* Create Time: 2011-9-29 22:21:29

* Mail: jbiaojerry@gmail.com

* Blog: http://blog.csdn.net/jerryjbiao

* Description: The third in the C ++ STL algorithm series interpreted by a simple program.

* Non-variable algorithm: Conditional search for the container element find_if

**************************************** **************************/

 

# Include <algorithm>

# Include <vector>

# Include <iostream>

 

Using namespace std;

 

// The predicate judgment function divbyfive: determines whether x can divide x by 5.

Bool divbyfive (int x)

{

Return x % 5? 0: 1;

}

 

Int main ()

{

// Initial vector

Vector <int> iVect (20 );

For (size_t I = 0; I <iVect. size (); ++ I)

{

IVect [I] = (I + 1) * (I + 3 );

}

 

Vector <int>: iterator iLocation;

ILocation = find_if (iVect. begin (), iVect. end (), divbyfive );

 

If (iLocation! = IVect. end ())

{

Cout <"the first element that can be divisible by 5 is :"

<* ILocation <endl // print element: 15

<"The index position of an element is :"

<ILocation-iVect. begin () <endl; // print the index location: 2

}

Return 0;

}

From: Jerry Jiang's program life

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.