equal用法


 
 
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;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值