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: simple programs interpret C ++ STL algorithm series 3 * Non-Variable Algorithms: conditional search for the container element find_if *********************************** * *****************************/# include <Algorithm> # Include <vector> # include <iostream> using namespace STD; // predicate judgment function divbyfive: determines whether X can divide bool divbyfive (int x) {return X % 5? 0: 1;} int main () {// initial vectorvector <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 to be divisible by 5 is:" <* ilocation <Endl // print element: 15 <"the index position of the element is:" <ilocation-ivect. begin () <Endl; // print index location: 2} return 0 ;}

**************************************** **************************************** **************************************** *******

C ++ classic bibliography index and resource download:Http://blog.csdn.net/jerryjbiao/article/details/7358796

**************************************** **************************************** **************************************** ********

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.