注意:在写这个问题时,我想我已经找到了答案.随意修改或附加更好的版本.我认为记录我的问题可能会很好.编辑我错了,我的aswer不正确.
考虑一个整数对列表:我想根据部分排序对它们进行拓扑排序.这类似于偏序,与总数相比,足以构建堆?,但我想使用std :: sort而不是std :: priority_queue.
为此,我写了这段代码:
#include <iostream>
#include <vector>
#include <algorithm>
struct pair {
int a, b;
pair(int a, int b) : a(a), b(b) {}
std::ostream &print(std::ostream &out) const {
return (out << "(" << a << ", " << b << ")");
}
};
std::ostream &operator<<(std::ostream &out, const pair &p) { return p.print(out); }
struct topological_pair_comparator {
bool operator()(const pair &p, const pair &q) const { return p.a<q.a && p.b<q.b; }
} …Run Code Online (Sandbox Code Playgroud) 我想用std::map我的班级到另一个班级的地图.如果我尝试以下代码,我会收到错误" undefined operator <".这是否意味着我需要课堂K上的订购才能使用map?它必须是完整的订购吗?我需要所有四个订购操作员还是>足够了?
#include <iostream>
#include <map>
#include <stdio.h>
using namespace std;
struct K {
int i, j;
K(int i, int j) : i(i), j(j){}
friend bool operator==(const K& x, const K& y){ return (x.i==y.i)&&(x.j==y.j); }
friend bool operator!=(const K& x, const K& y){ return !(x==y); }
/* friend bool operator<(const K&x, const K&y){
if(x.i<y.i) return true;
if(x.i>y.i) return false;
return x.j<y.j;
}
friend bool operator>(const …Run Code Online (Sandbox Code Playgroud)