make_heap comparison

This is a discussion on make_heap comparison within the C++ Programming forums, part of the General Programming Boards category; Hi there. I want to turn a vector of structures in a heap. I'm trying to use make_heap function, from ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    14

    make_heap comparison

    Hi there.

    I want to turn a vector of structures in a heap. I'm trying to use make_heap function, from STL which have the following constructor:

    Code:
    template <class RandomAccessIterator, class Compare>
    void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
    I realy need to build that comp class so I can compare those structure's fields but I realy don't know how. I've searched a lot and didn't find anything useful about that Compare class.

    Can anyone teach me how to build one?

    Thank you for any help.

    Regards.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,783
    The are two versions of make_heap: one uses the operator< of your class, the other allows you to specify a comparator. "Compare" is just the placeholder for the name of your function or function object. Likewise, "RandomAccessIterator" is just a placeholder telling you that the first two arguments take a random access iterator, be it a pointer or the iterator returned by the begin() and end() member functions of a vector.

    So, if you already have an operator< that makes sense in this context, then use that version of make_heap that has only two arguments. If you do not want to use operator<, then you may try a function object, e.g.,
    Code:
    class CompareFoo
    {
    public:
        // Compare two Foo structs.
        bool operator()(const Foo& x, const Foo& y) const
        {
            return x.bar < y.bar;
        }
    };
    
    // Create an instance of CompareFoo to use in comparision.
    make_heap(v.begin(), v.end(), CompareFoo());
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    Thanks, that really helped.

    Regards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  3. Function pointer comparison
    By matsp in forum C Programming
    Replies: 14
    Last Post: 08-28-2008, 10:05 AM
  4. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  5. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 12:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21