[英]C++ STL make_heap and pop_heap not working
I need to use a Heap, so i've searched about the STL one, but it doesn't seem to work, i wrote some code to explain what i mean: 我需要使用堆,所以我搜索了STL,但它似乎没有用,我写了一些代码来解释我的意思:
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
struct data
{
int indice;
int tamanho;
};
bool comparator2(const data* a, const data* b)
{
return (a->tamanho < b->tamanho);
}
int main()
{
std::vector<data*> mesas;
data x1, x2, x3, x4, x5;
x1.indice = 1;
x1.tamanho = 3;
x2.indice = 2;
x2.tamanho = 5;
x3.indice = 3;
x3.tamanho = 2;
x4.indice = 4;
x4.tamanho = 6;
x5.indice = 5;
x5.tamanho = 4;
mesas.push_back(&x1);
mesas.push_back(&x2);
mesas.push_back(&x3);
mesas.push_back(&x4);
mesas.push_back(&x5);
make_heap(mesas.begin(), mesas.end(), comparator2);
for(int i = 0 ; i < 5 ; i++)
{
data* mesa = mesas.front();
pop_heap(mesas.begin(),mesas.end());
mesas.pop_back();
printf("%d, %d\n", mesa->indice, mesa->tamanho);
}
return 0;
};
and this is what i get: 这就是我得到的:
4, 6
2, 5
1, 3
3, 2
5, 4
So it's not working as a heap, as the maximum element on the vector is not being returned right. 所以它不是作为堆工作,因为向量上的最大元素没有正确返回。
Or am i doing something wrong? 或者我做错了什么?
You need to pass comparator2
to std::pop_heap
since that is how you created the heap. 您需要将
comparator2
传递给std::pop_heap
因为这是您创建堆的方式。 Otherwise, it will use the default less than operator for pointers, which simply compares the pointer values. 否则,它将使用默认的小于运算符的指针,它只是比较指针值。
MSN's answer is correct. MSN的答案是正确的。 However, either of a couple style guidelines can prevent this error:
但是,两种风格指南中的任何一种都可以防止此错误:
Declare the comparator to work on references, not objects, as operator<
would. 声明比较器用于引用而不是对象,作为
operator<
would。 Use a vector
of objects, not pointers. 使用对象
vector
,而不是指针。
bool comparator2(const data& a, const data& b) { return (a.tamanho < b.tamanho); }
You might really need the vector of pointers, in which case this doesn't apply. 您可能真的需要指针向量,在这种情况下这不适用。
Use std::priority_queue
(from <queue>
), which ties together pop_heap
and pop_back
for you, remembering your comparator. 使用
std::priority_queue
(来自<queue>
),它将pop_heap
和pop_back
联系在一起,记住你的比较器。 This requires a functor comparator: 这需要一个仿函数比较器:
struct comparator2 { bool operator()(const data& a, const data& b) { return (a.tamanho < b.tamanho); } }; std::priority_queue<data, vector<data>, comparator2> mesas; // or std::priority_queue<data, vector<data>, comparator2> mesas.push(x1);
Most elegant way is to make this the default comparison for data
: 最优雅的方法是将其作为
data
的默认比较:
struct data { int indice; int tamanho; friend bool operator<(const data& a, const data& b) { return (a.tamanho < b.tamanho); } }; std::priority_queue<data> mesas; mesas.push(x1);
priority_queue
can also take a prefilled unsorted container, which it will copy. priority_queue
还可以使用预先填充的未分类容器,它将复制。
I had a similar problem and was able to solve it with something like this: 我有一个类似的问题,并能够解决这个问题:
struct comparator2 { bool operator()(data const * const a, data const * const b)
{
return (a->tamanho < b->tamanho);
} };
std::priority_queue<data*, std::vector<data*>, comparator2> mesas;
What about std::set 那么std :: set呢
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <set>
struct data
{
// Always put constructors on.
// When the constructor is finished the object is ready to be used.
data(int i,int t)
:indice(i)
,tamanho(t)
{}
int indice;
int tamanho;
// Add the comparator to the class.
// Then you know where to look for it.
bool operator<(data const& b) const
{
return (tamanho < b.tamanho);
}
};
int main()
{
std::set<data> mesas;
// Dont declare all your variables on the same line.
// One per line otherwise it is hard to read.
data x1(1,3);
data x2(2,5);
data x3(3,2);
data x4(4,6);
data x5(5,4);
mesas.insert(x1);
mesas.insert(x2);
mesas.insert(x3);
mesas.insert(x4);
mesas.insert(x5);
// You don't actually need the variables.
// You could have done it in place.
mesas.insert(data(6,100));
// Use iterator to loop over containers.
for(std::set<data>::iterator loop = mesas.begin(); loop != mesas.end(); ++loop)
{
printf("%d, %d\n", loop->indice, loop->tamanho);
}
return 0;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.