English 中文(简体)
C++ STL 使得无法工作的人能够从事肥皂和工作。
原标题:C++ STL make_heap and pop_heap not working
  • 时间:2010-05-11 21:26:06
  •  标签:
  • c++
  • stl
  • heap

我需要使用“Heap”,这样一来就检索了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
", mesa->indice, mesa->tamanho);
        }

    return 0;
};

而这正是:

4, 6
2, 5
1, 3
3, 2
5, 4

因此,由于病媒上的最大元素没有被退回,因此它不会发挥巨大的作用。

或者说是错了吗?

问题回答

需要通过<代码>comparator2至std:pop_heap<>code。 既然如此,你是如何创造出的。 否则,它将使用低于操作者的缺省,仅仅比较点值。

MSN的答案是正确的。 然而,两种风格的准则都能够防止这一错误:

  • 宣布参照国而非标的参照国为operator<。 使用<条码>物体,而不是点码。

    bool comparator2(const data& a, const data& b)
    {
        return (a.tamanho < b.tamanho);
    }
    

    你可能真的需要点击器的载体,在这种情况下,这并不适用。

  • 使用<代码>std: 优先权_queue(从<queue>pop_heappop_back,供您参考。 这需要一位分析员:

    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);
    
  • 最常见的方法是将这一缺省比较用于<条码>数据:

    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);
    

也可使用一个未填充的集装箱,将予以复制。

st:

#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
", loop->indice, loop->tamanho);
        }

    return 0;
};

我有同样的问题,能够以这样的方式解决:

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;




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签