本文主要是介绍Equal()函数的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
函数语法:
1、template <class InputIterator1, class InputIterator2>bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 );2、template <class InputIterator1, class InputIterator2, class BinaryPredicate>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 versionreturn 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().
返回值:
- #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;
- }
- }
这篇关于Equal()函数的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!