- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图解决一个编码问题,该问题需要我使用堆按特定顺序获取项目。当我尝试同时使用自定义 priority_queue 和 make_heap 来实现解决方案时,我发现我们传入自己的比较器的方式不同且令人困惑。
c++.com 说:
priority_queue (const Compare& comp = Compare(), const Container& ctnr = Container());
Comp 是用于对堆进行排序的比较对象。这可能是一个函数指针或函数对象,能够通过比较它的两个参数来执行严格的弱排序。
void make_heap (RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
这里的 comp 是一个二元函数,它接受范围内的两个元素作为参数,并返回一个可转换为 bool 的值。这可以是函数指针或函数对象。
我对这里使用 2 comp 的区别感到困惑,当我尝试实现我的代码时:
// first I define a functor and a static function, both used to be passed as the comp argument;
class isGreater {
public:
isGreater() {}
inline bool operator() (const ListNode* l1, const ListNode* l2) const {
return l1->val >= l2->val;
}
};
static bool isLarger(const ListNode* l1, const ListNode* l2) {
return l1->val >= l2->val;
}
// then I tried several ways to define a custom pq and heap:
// correct
priority_queue<ListNode*, std::vector<ListNode*>, isGreater> p;
// wrong, error: data member instantiated with function type
// 'value_compare'(aka 'isGreater ()')
priority_queue<ListNode*, std::vector<ListNode*>, isGreater()> p;
// wrong, passing a function pointer like this is not allowed
// error: template argument for template type parameter must be a type
priority_queue<ListNode*, std::vector<ListNode*>, isLarger> p;
// correct
make_heap(v.begin(), v.end(), isGreater());
// correct, here passing a function pointer like this is allowed
make_heap(v.begin(), v.end(), isLarger);
// wrong, the "()" is needed, different from how we define priority queue
make_heap(v.begin(), v.end(), isGreater);
我对我们将 comp 作为参数传递的方式的差异感到困惑。有人可以帮我解决这个难题吗?
最佳答案
为了理解发生了什么,您需要理解 isGreater
和 isGreater()
之间的区别
isGreater
是类型的名称。它可以到达类型可以到达的地方,即进入模板参数(三角括号)和声明。isGreater()
是一个构造函数调用,它生成一个类型为 isGreater
的对象。它可以出现在表达式可以出现的任何地方,例如它可以用作函数参数。现在错误应该很清楚了:不允许使用类型名称 isGreater
代替对象表达式 isGreater()
,反之亦然。
关于c++ - 对定义自定义优先级队列和使用 C++ 中的 make_heap 等方法初始化堆之间的区别感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38537277/
我有一个程序将另一个 vector 的子集堆排序到一个 vector 中,作为对索引的引用。 std::vector knowledgeBase; std::vector workingSet; 这个
我有一个 std::vector v , t_Mont有 {float val, int i and int j} 我想做make_heap(v.begin(), v.end())和 pop_head
我必须为 make_heap 重载什么运算符?是 () 运算符吗?如果我已经在我的算法中为另一个案例定义了那个。谁能提供在我的案例中使用 make_heap 的正确方法。请参阅下面的代码以更好地理解。
我对 vector 和迭代器有基本的了解。但是我在理解以下代码片段的输出时遇到了问题。 具体来说,我无法找出 make_heap() 函数的功能。它是如何产生输出的:91 67 41 24 59 32
编译如下: class Compare { bool cmp(const int& a, const int& b){return a>b;} }; int main() { vector
有人能告诉我像 std::make_heap 这样的 STL 堆函数模板的意义吗?为什么有人会使用它们?有实际用途吗? 最佳答案 算法和数据结构方面的类(class)会很好地回答您的直接问题。在计算机
关于make_heap函数的问题。 使用 make_heap 在 STL 中创建堆时它需要 v.begin() 和 v.end()//[front, end) 但是如果我不想在堆中添加 10 怎么办.
make_heap() 可以用 vector 中的一对来实现吗? 我正在使用: std::vector > lista_abierta_; 我使用对象函数按第一个成员对对排序,但它崩溃了。 代码是:
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我有一个要用来创建堆的 vector 。我不确定是否应该使用 C++ make_heap 函数或将 vector 放入优先级队列?在性能方面哪个更好?我应该什么时候使用一个与另一个? 最佳答案 在性能
STL 有这个非常有用的功能。 Java 中是否有与此等效的内容?我可以编写我的自定义实现,但不要重新发明轮子会很有用。 最佳答案 std::make_heap 创建一个二进制堆,用作优先级队列,称为
我试图解决一个编码问题,该问题需要我使用堆按特定顺序获取项目。当我尝试同时使用自定义 priority_queue 和 make_heap 来实现解决方案时,我发现我们传入自己的比较器的方式不同且令人
我有一个这样定义的 map std::map myMap; 处理完这个映射后,我想把它当作一个堆(基于第二个值)。我决定使用 std::make_heap 函数……定义如下…… template vo
我试图找到有关此特定用途的信息,但找不到。 在一个模拟软件(就像一个视频游戏)中,我正在构建一个事件管理器,它根据事件创建者提交的值来确定事件的优先级。事件队列每帧将被解析一次,其中的所有事件都将被处
如何使用 make_heap 创建最小堆 中的方法 来自documentation它说,我们可以传入第三个参数 Compare comp 那是一个 Binary function that accep
#include #include using namespace std; template bool HeapComparator (T,T); template void PrintArra
我正在尝试使用 make_heap 通过堆实现 Dijkstra 算法,它(希望)按递减顺序对 vector 进行排序,以便创建优先级队列,但出于某种原因,如果我更改值并且我不知道为什么,排序就会出错
我需要使用堆,所以我搜索了 STL,但它似乎不起作用,我写了一些代码来解释我的意思: #include #include #include #include struct data {
这是我的代码: std::priority_queue, SimpleCycle> pq; pq.push(cycle1); pq.push(cycle2);
我查看了 C++0x 标准,发现 make_heap 的比较次数不应超过 3*N。 IE。 heapify 无序集合可以在 O(N) 中完成 /* @brief Construct a he
我是一名优秀的程序员,十分优秀!