set_difference stl等算法的使用要注意 比較函數的書寫方式,例子以下:算法
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <math.h>
struct CJpoint
{
double x,y;函數
CJpoint(double xx, double yy){
x = xx;
y = yy;
};
CJpoint(const CJpoint& o){
x= o.x;
y = o.y;
};it
bool operator < (const CJpoint &a)const{
return x==a.x ? y<a.y : x<a.x;
}gc
bool operator == (const CJpoint &a)const{
return x==a.x && y==a.y;
}stl
bool operator != (const CJpoint &a)const{
return x!=a.x || y!=a.y;
}
};
bool ComparePs(CJpoint* a,CJpoint* b)
{
if( ((a->x-b->x)*(a->x-b->x) + (a->y-b->y)*(a->y-b->y)) < 0.00000000001)
return false;sort
return true;
}di
bool ComparePsw(CJpoint* a,CJpoint* b)
{
return *a < *b;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<CJpoint*> vec1,vec2,vec3;
CJpoint *p1= new CJpoint(1,2);
CJpoint *p2 = new CJpoint(3,4);
CJpoint *p3 = new CJpoint(5,7);
CJpoint *p4 = new CJpoint(5,7);
CJpoint *p5 = new CJpoint(6,4);
CJpoint *p6 = new CJpoint(7,7);
vec1.push_back(p1);
vec1.push_back(p2);
vec1.push_back(p3);
vec2.push_back(p4);
vec2.push_back(p5);
vec2.push_back(p6);poi
std::sort(vec1.begin(),vec1.end(),ComparePsw);
std::sort(vec2.begin(),vec2.end(),ComparePsw);
set_difference(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(),std::inserter(vec3, vec3.begin()), ComparePsw );
//std::vector<CJpoint*>::iterator it = vec.begin();co
//double pvalue =0;
//pvalue = (*it)->x;
//pvalue = (*(it+1))->x;
//delete p1,p2;
return 0;
}let