c++ - 成对的 std::make_heap

标签 c++ data-structures std-pair

make_heap() 可以用 vector 中的一对来实现吗?

我正在使用:

 std::vector< std::pair < int , tablero& > > lista_abierta_;

我使用对象函数按第一个成员对对排序,但它崩溃了。

代码是:

#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <functional>
#include "8_puzzle.h"
#include "tablero.h"

using namespace std;

class comp {
public:
    bool operator()(pair < int, tablero&> a, pair < int, tablero&> b) const {
        return a.first > b.first;
    }
};

pair < int, tablero& > puzzle::A_estrella::tope()
{
    pair < int, tablero& > l=lista_abierta_.front();

    pop_heap(lista_abierta_.begin(),lista_abierta_.end());
    lista_abierta_.pop_back();

    return l;
}

[取自here ]

最佳答案

只要std::pair<T, U>提供 operator< (意思是:TU 提供 operator< ),我没有发现使用 make_heap 有任何问题。

关于c++ - 成对的 std::make_heap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4135834/

相关文章:

C++命名: read_input() vs. readInput()

c++ - 为字符数组C++分配额外的内存

c++ - 当您在 SDL2 中移动窗口时,如何解决程序卡住问题?

java - 如何在迭代时在常数时间内修改 Java 链表中的元素?

c++ - 在索引和值类型上参数化的惯用 vector 类型

c++ - 如何为字符串创建一个 unordered_map 函数

c++ - 如何通过 shellexecute (visual studio c++/mfc) 运行 java 类

algorithm - 排序单循环链表

C++:将成对的类对象插入到映射中

c++ - std::pair assignment with downcast