Equal()函数的用法

函数语法

 
  
1、template 
   bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 );

2、template 
   bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred );
函数用法:

测试是否在两个范围的元素都是平等的

 
  

Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if the elements in both ranges are considered equal.

The elements are compared by either applying the == comparison operator to each pair of corresponding elements, or the template parameter comp (for the second version).

这个函数模板的行为等于:

template <class InputIterator1, class InputIterator2>
  bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
  while ( first1!=last1 )
  {
    if (*first1 != *first2)   // or: if (!pred(*first1,*first2)), for pred version
      return false;
    ++first1; ++first2;
  }
  return true;
}
参数注释:
   first1, last1:
      Forward iterators to the initial and final positions of the first sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.           

first2:

Forward iterator to the initial position of the second sequence. The comparison includes up to as many elements in this sequence as in the above sequence.

pred:

Binary predicate taking two elements as argument (one of each of the two sequences), and returning the result of the comparison between them, with true (non-zero) meaning that they are to be considered equal, and false (zero) that they are not-equal. This can either be a pointer to a function or an object whose class overloads operator().

返回值:
 
   
  1. #include  
  2. #include  
  3. #include  
  4. using namespace std;  
  5. class student{  
  6.     public:  
  7.         int No,grade;  
  8.         student(int No,int grade):No(No),grade(grade){};  
  9.         bool operator==(student& s){  
  10.             return this->grade==s.grade;  
  11.         }  
  12. };  
  13. int main(){  
  14.     vectorv1;  
  15.     student s1(1000,100);  
  16.     student s2(1001,98);  
  17.     student s3(1002,96);  
  18.     student s4(1003,94);  
  19.     v1.push_back(s1);  
  20.     v1.push_back(s2);  
  21.     v1.push_back(s3);  
  22.     v1.push_back(s4);  
  23.     vectorv2;  
  24.     v2=v1;  
  25.     if(equal(v1.begin(),v1.end(),v2.begin())){  
  26.         cout<<"yes"<
  27.     }  
  28. }  
 
   

true if all the elements in the range [first1,last1) compare equal to those of the range starting at first2, and false otherwise.

  1. #include  
  2. #include  
  3. #include  
  4. using namespace std;  
  5. class student{  
  6.     public:  
  7.         int No,grade;  
  8.         student(int No,int grade):No(No),grade(grade){};  
  9.     /*  bool operator==(student& s){ 
  10.             return this->grade==s.grade; 
  11.         }*/  
  12. };  
  13. bool cmp(student& s1,student& s2){  
  14.     return s1.grade==s2.grade;  
  15. }  
  16. int main(){  
  17.     vectorv1;  
  18.     student s1(1000,100);  
  19.     student s2(1001,98);  
  20.     student s3(1002,96);  
  21.     student s4(1003,94);  
  22.     v1.push_back(s1);  
  23.     v1.push_back(s2);  
  24.     v1.push_back(s3);  
  25.     v1.push_back(s4);  
  26.     vectorv2;  
  27.     v2=v1;  
  28.     if(equal(v1.begin(),v1.end(),v2.begin(),cmp)){  
  29.         cout<<"yes"<
  30.     }  
  31. }  

你可能感兴趣的:(C++)