Simple Program interpretation of C ++ STL algorithm series 4: adjacent_find

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 adjacent_find algorithm is used to find adjacent element pairs that meet equal or equal conditions. There are two types of function prototypes: one is to find the iterator position of the first element in the element pair when two consecutive elements are equal in the iterator range [first, last. The other is to use binary predicates to determine binary_pred, and find the adjacent element pairs that meet the binary_pred condition on the iterator range [first, last). If not found, return last.

Function prototype:

template<class ForwardIterator>   ForwardIterator adjacent_find(      ForwardIterator _First,       ForwardIterator _Last      );template<class ForwardIterator , class BinaryPredicate>   ForwardIterator adjacent_find(      ForwardIterator _First,       ForwardIterator _Last,             BinaryPredicate _Comp   );

Sample Code:

/*************************************** * **************************** Copyright (c) jerry Jiang * file name: adjacent_find.cpp * Author: Jerry Jiang * Create Time: 2011-9-30 22:07:22 * mail: jbiaojerry@gmail.com * blog: http://blog.csdn.net/jerryjbiao * description: A simple program interprets the four non-Variable Algorithms in the C ++ STL algorithm series: find the container element adjacent_find *********************************** * *****************************/# includ E <algorithm> # include <list> # include <iostream> using namespace STD; // checks whether X and Y are parity bool parity_equal (int x, int y) {return (x-y) % 2 = 0? 1: 0;} int main () {// initialize the list of linked lists <int> ilist; ilist. push_back (3); ilist. push_back (6); ilist. push_back (9); ilist. push_back (11); ilist. push_back (11); ilist. push_back (18); ilist. push_back (20); ilist. push_back (20); // output list <int>: iterator ITER; For (iter = ilist. begin (); iter! = Ilist. end (); ++ ITER) {cout <* ITER <";}cout <Endl; // query the list of elements with the same adjacent areas <int> :: iterator iresult = adjacent_find (ilist. begin (), ilist. end (); If (iresult! = Ilist. end () {cout <"the first pair of equal Adjacent Elements in the linked list is:" <Endl; cout <* iresult ++ <Endl; cout <* iresult <Endl;} // find the adjacent element iresult = adjacent_find (ilist. begin (), ilist. end (), parity_equal); If (iresult! = Ilist. end () {cout <"the first pair of elements with the same parity in the linked list is:" <Endl; cout <* iresult ++ <Endl; cout <* iresult <Endl;} 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.