Simple Program interpretation of C ++ STL algorithm series 9: equal

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 equal algorithm is similar to mismatch. The Equal algorithm also compares the elements of the two sequences one by one, but the equal function returns the bool value true/false instead of the iterator value. It has the following two prototypes: If the iterator interval [first1, last1) and the iterator interval [first2, first2 + (last1-first1 )) returns true if the element on is equal (or the condition binary_pred meets the condition of binary predicates). Otherwise, false is returned.

Function prototype:

template<class InputIterator1, class InputIterator2>   bool equal(      InputIterator1 _First1,       InputIterator1 _Last1,       InputIterator2 _First2      );template<class InputIterator1, class InputIterator2, class BinaryPredicate>   bool equal(      InputIterator1 _First1,       InputIterator1 _Last1,       InputIterator2 _First2,       BinaryPredicate _Comp      ); 

Example program:

Using Binary predicates to determine the condition absequal, we can determine that the absolute values of elements in two vector containers are equal.

/*************************************** * **************************** Copyright (c) jerry Jiang * file name: equal. CPP * Author: Jerry Jiang * Create Time: 2011-10-10 22:51:07 * mail: jbiaojerry@gmail.com * blog: http://blog.csdn.net/jerryjbiao * Description: simple program interpretation of C ++ STL algorithm series of nine * non variable algorithm: equal element judgment equal ************************************ * ****************************/# include <algorithm> # In Clude <vector> # include <iostream> using namespace STD; bool absequal (int A, int B) {return (A = ABS (B) | B = ABS ())? True: false;} int main () {vector <int> ivect1 (5); vector <int> ivect2 (5); For (vector <int> :: size_type I = 0; I <ivect1.size (); ++ I) {ivect1 [I] = I; ivect2 [I] = (-1) * I ;} if (equal (ivect1.begin (), ivect1.end (), ivect2.begin (), absequal) {cout <"the absolute values of ivect1 and ivect2 are completely equal" <Endl ;} else {cout <"the absolute values of the ivect1 and ivect2 elements are not completely equal" <Endl;} return 0 ;}

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

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

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

 

 

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.