相关疑难解决方法(0)

使用std :: sort进行拓扑排序

注意:在写这个问题时,我想我已经找到了答案.随意修改或附加更好的版本.我认为记录我的问题可能会很好.编辑我错了,我的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)

c++ sorting topological-sort c++11

3
推荐指数
1
解决办法
2147
查看次数

我是否需要在K类上进行完整排序才能使用std :: map <K,L>

可能重复:
std :: map键类必须满足哪些要求才能成为有效键?

我想用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)

c++ class std map

2
推荐指数
1
解决办法
184
查看次数

标签 统计

c++ ×2

c++11 ×1

class ×1

map ×1

sorting ×1

std ×1

topological-sort ×1