std::find 结构体比较


#include "stdafx.h"
#include "iostream"
#include "vector"
using namespace std;

class Compare //比较类
{
public:
	Compare(int a = 0, int b = 0) : _a(a), _b(b) //构造函数
	{
	}
	
	bool operator == ( const Compare& n) //重写===运算符
	{
		return _a == n._a && _b == n._b;
	}
private:
	int _a;
	int _b;
};

int main()
{
	vector<Compare> ComparyArray; 

	ComparyArray.push_back(Compare(1, 1));
	ComparyArray.push_back(Compare(2, 2));
	ComparyArray.push_back(Compare(3, 3));
	ComparyArray.push_back(Compare(4, 4));
	ComparyArray.push_back(Compare(5, 5));

	Compare Tmp = Compare(5, 5);

	vector<Compare>::iterator it = find(ComparyArray.begin(), ComparyArray.end(), Tmp);
	if (it != ComparyArray.end())
	{
		cout << "找到了" << endl;
	}
	else
	{
		cout << "未找到了" << endl;
	}

	return 0;
}

 

发布了15 篇原创文章 · 获赞 9 · 访问量 8317
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览