make_heap 不创建堆
我有一个程序,可以对向量中另一个向量的子集进行堆排序,作为对索引的引用。
std::vector<foo> knowledgeBase;
std::vector<int> workingSet;
这个比较类有效吗?
class Compare
{
bool operator()(int lft, int rgt)
{
return knowledgeBase[lft].bar() > knowledgeBase[rgt].bar();
}
};
Compare 是包含knowledgeBase 的类中的嵌套类,因此我可以访问变量,但workingSet.front() 永远不会返回引用最小值的索引;
我做错了什么?如果需要,我可以发布更多代码(其中有更多不相关的错误,我无法测试这些错误,因为这不起作用),但我确信我的 make_heap 没有创建我想要的堆。
如果我做了一些非常愚蠢的事情,我的 make_heap 调用如下:
std::make_heap(workingSet.begin(), workingSet.end(), Compare());
编辑: bar 是 foo 内部的 std::set 的 size() 。该集合不是空的,也不是未定义的,因为我可以输出其内容(并验证它们是否正确)。虽然这是使用迭代器......这还不够吗?
Edit2:经过进一步研究,我发现 bar() 总是返回 1。我添加了一个 int,并在每次添加变量时递增它,就像这样...
foo::foo()
{
siz = 0;
}
void foo::addLiteral(std::string var, bool truth)
{
literals.insert(Literal(var,truth));
++siz;
}
class foo()
{
public:
foo();
void addLiteral(std::string var, bool truth);
bool bar(){return siz;}
private:
int siz;
std::set<Literal, LiteralComp> literals;
}
foo 的初始化如下:
...
foo newClause;
ss.str(input);
ss >> variable;
while(!ss.fail())
{
if(variable[0] == '~')
{
variable = variable.substr(1);
truth = false;
}
else truth = true;
newClause.addLiteral(variable, truth);
ss >> variable;
}
knowledgeBase.push_back(newClause);
workingSet.push_back(count++);
...
和 foo.size () 仍然总是返回 1。
这是怎么回事?
我意识到这远远超出了我的主要问题的范围,并且我给出的代码中没有定义一些部分,但我已经在这个问题上工作了六个小时左右,但仍然不知道发生了什么在。
I have a program that heapsorts a subset of another vector in a vector, as references to indices.
std::vector<foo> knowledgeBase;
std::vector<int> workingSet;
Does this comparison class work?
class Compare
{
bool operator()(int lft, int rgt)
{
return knowledgeBase[lft].bar() > knowledgeBase[rgt].bar();
}
};
Compare is a nested class within the class that contains knowledgeBase, so I have access to the variables, but the index referring to the smallest value is never returned by workingSet.front();
What am I doing wrong? I can post more code if required, (which has further, unrelated bugs that I can't test for because this doesn't work) but I know for certain that my make_heap is not creating the heap that I want.
In case I'm doing something really stupid, my make_heap call is as follows:
std::make_heap(workingSet.begin(), workingSet.end(), Compare());
Edit: bar is size() of a std::set internal to foo. This set is not empty, nor is it undefined, because I can output its contents (and verify them as correct). Though that is using an iterator... is that not sufficient?
Edit2: Upon further research, I found that bar() was always returning 1. I added an int, and incremented that every time I added a variable, like so...
foo::foo()
{
siz = 0;
}
void foo::addLiteral(std::string var, bool truth)
{
literals.insert(Literal(var,truth));
++siz;
}
class foo()
{
public:
foo();
void addLiteral(std::string var, bool truth);
bool bar(){return siz;}
private:
int siz;
std::set<Literal, LiteralComp> literals;
}
foo is initialized like so:
...
foo newClause;
ss.str(input);
ss >> variable;
while(!ss.fail())
{
if(variable[0] == '~')
{
variable = variable.substr(1);
truth = false;
}
else truth = true;
newClause.addLiteral(variable, truth);
ss >> variable;
}
knowledgeBase.push_back(newClause);
workingSet.push_back(count++);
...
And foo.size()
still always returns 1.
What is going on?
I realize this is far out of the scope of my main question, and there are pieces not defined in the code I've given, but I've been working at this problem for six or so hours now and still have no idea what's going on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
......我现在感觉很愚蠢。
很抱歉浪费了任何人的时间。
... I feel dumb now.
Sorry for wasting anyone's time.
您没有忘记调整工作集的大小吧?例如,先使用 Reserve,然后使用
operator[]
不会增加向量的实际大小,因此make_heap
将无操作。You didn't forget to size your working set did you? For example, using reserve and then
operator[]
won't increase the actual size of the vector and somake_heap
will no-op.