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 pred ); | <algorithm> |
Test whether the elements in two ranges are equal
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).
The behavior of this function template is equivalent to:
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; } |
Parameters
-
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().
Return value
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class student{
public:
int No,grade;
student(int No,int grade):No(No),grade(grade){};
bool operator==(student& s){
return this->grade==s.grade;
}
};
int main(){
vector<student>v1;
student s1(1000,100);
student s2(1001,98);
student s3(1002,96);
student s4(1003,94);
v1.push_back(s1);
v1.push_back(s2);
v1.push_back(s3);
v1.push_back(s4);
vector<student>v2;
v2=v1;
if(equal(v1.begin(),v1.end(),v2.begin())){
cout<<"yes"<<endl;
}
}
true if all the elements in the range [first1,last1) compare equal to those of the range starting at first2, and false otherwise.
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class student{
public:
int No,grade;
student(int No,int grade):No(No),grade(grade){};
/* bool operator==(student& s){
return this->grade==s.grade;
}*/
};
bool cmp(student& s1,student& s2){
return s1.grade==s2.grade;
}
int main(){
vector<student>v1;
student s1(1000,100);
student s2(1001,98);
student s3(1002,96);
student s4(1003,94);
v1.push_back(s1);
v1.push_back(s2);
v1.push_back(s3);
v1.push_back(s4);
vector<student>v2;
v2=v1;
if(equal(v1.begin(),v1.end(),v2.begin(),cmp)){
cout<<"yes"<<endl;
}
}