首页 > 其他 > 详细

Effective STL第十九节 相等和等价的区别

时间:2015-03-24 09:15:56      阅读:210      评论:0      收藏:0      [点我收藏+]
#include <set>
#include <vector>
#include <iostream>
using namespace std;

class mPoint
{
public:
	int x;
	int y;

	mPoint();

	mPoint(int tx, int ty)
	{
		x = tx;
		y = ty;
	}

	bool operator ==( const mPoint& rhs) const
	{
		return this->x == rhs.x && this->y==rhs.y;			//相等性 a.x==b.x && a.y==b.y才认为相等
	}
	
};

class setCmp 
{
public:
	bool operator() (const mPoint& lhs, const mPoint& rhs) const
	{
		return lhs.x < rhs.x;
	}
};

void fillSet(set<mPoint,setCmp> & paraSet)
{
	paraSet.insert(mPoint(1, 2));			//insert函数使用等价性来插入,在本例中如果a.x>b.x==false && a.x<b.x==false 则认为a和b等价	
	paraSet.insert(mPoint(1, 3));			//此时临时对象mPoint(1,3)不会被插入
	paraSet.insert(mPoint(1, 4));			//临时对象mPoint(1,4)也不会插入	
}


int main( )
{
	set<mPoint,setCmp> mSet;
	fillSet(mSet);

	set<mPoint,setCmp>::iterator its=find( mSet.begin(),mSet.end(),mPoint(1,3));
	if (its!=mSet.end())
	{
		cout << "全局算法find使用使用相等性查找出的值为" << "mPoint.x=" << (*its).x << "   " << "mPoint.y=" << (*its).y << endl;
	}
	else
	{
		cout << "全局算法使用相等性查找未查找到想要的值" <<endl;
	}

	its = mSet.find(mPoint(1, 3));
	if (its != mSet.end())
	{
		cout << "关联性容器自带算法find使用使用等价性查找出的值为" << "mPoint.x=" << (*its).x << "   " << "mPoint.y=" << (*its).y << endl;
	}
	else
	{
		cout << "关联性容器算法使用等价性查找未查找到想要的值" << endl;
	}

	return 0;
}

Effective STL第十九节 相等和等价的区别

原文:http://blog.csdn.net/wks19891215/article/details/44576217

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!