简体   繁体   English

make_heap不堆

[英]make_heap not making heaps

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(); Compare是包含知识基础的类中的嵌套类,因此我可以访问变量,但是引用最小值的索引永远不会由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. 如果需要的话,我可以发布更多代码((这些代码还有其他一些无关的错误,我无法测试,因为这不起作用),但我可以肯定地知道我的make_heap不会创建我想要的堆。

In case I'm doing something really stupid, my make_heap call is as follows: 万一我做的事情很愚蠢,我的make_heap调用如下:

std::make_heap(workingSet.begin(), workingSet.end(), Compare());

Edit: bar is size() of a std::set internal to foo. 编辑:bar是foo内部std :: set的size()。 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... 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 is initialized like so: 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++);
...

And foo.size() still always returns 1. 并且foo.size()仍然始终返回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. 我意识到这超出了我的主要问题的范围,并且我给出的代码中还没有定义一些片段,但是我已经在这个问题上工作了大约六个小时,现在仍然不知道要怎么做上。

... I feel dumb now. ...我现在觉得很蠢。

class foo()
{
public:
    foo();
    void addLiteral(std::string var, bool truth);
    bool bar(){return siz;} <==== returns a bool
private:
    int siz;
    std::set<Literal, LiteralComp> literals;
}

Sorry for wasting anyone's time. 很抱歉浪费任何人的时间。

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 so make_heap will no-op. 例如,使用reserve然后使用operator[]不会增加向量的实际大小,因此make_heapmake_heap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM