我有一个向量,它拥有一个点向量,这是一个具有x和ay坐标的类。我基本上必须从向量中删除所有排列和子集。为此,我利用算法包括和is_permutation
我已经重载了'=='运算符,因此我们为什么需要它很有意义。但是除非我重载'<'运算符,否则这两种算法将不起作用。
这是我的重点课:
class Point{
private:
int x;
int y;
public:
Point(){
x=0;
y=0;
}
Point(int xx, int yy){
x=xx;
y=yy;
}
double getSlope(Point p){
if(this->x!=p.x && this->y!=p.y){
return (double)(double(this->y-p.y)/double(this->x-p.x));
}else if(this->x==p.x){
return INT_MAX;
}else{
return INT_MIN;
}
}
void print(){
cout<<"(" <<x<<","<<y<<")";
}
bool operator == (Point &p){
if(this->x==p.x && this->y==p.y )
return true;
else
return false;
}
bool operator < (Point &p){
cout<<"\nin the < operator\n";
if(this->x < p.x && this->y < p.y )
return true;
else
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
这是一个函数,它接收点的临时向量并将其与vector>进行比较以去除排列。这些点是从文件中获取的,因此当我们获得这些点时,如果它们通过了检查,我们只会将其添加到vector>中
bool check(PointVector temp, vector<PointVector> pointArrays ){
for(int i =0 ; i < pointArrays.size(); i++){
if(is_permutation(pointArrays[i].begin(),pointArrays[i].end(),temp.begin())){
return false;
}else if(includes(pointArrays[i].begin(),pointArrays[i].end(),temp.begin(),temp.end())){
return false;
}else if(includes(temp.begin(),temp.end(),pointArrays[i].begin(),pointArrays[i].end())){
pointArrays[i].swap(temp);
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
点向量是一个typedef vector<points>;
这是关于std::includes,这对要排序的输入序列提出了要求(根据比较器- operator<)。
在此前提下,可以使用operator==(具有not(<和>)的语义)和相同的线性渐进复杂度来实现该算法。如果没有排序的要求(以及第一个范围的随机访问迭代器),它将为O(n log m)。
但是,有了operator<,我们可以更快地决定是否false要从算法中返回,因为我们已经从第二个序列中读取了一个元素,该元素位于第一个序列中的下一个元素之前。
In short, algorithms that deal with ordered sequences work in terms of ordered comparators.