Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7784377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:05:09+00:00 2026-06-01T20:05:09+00:00

What operator do I have to overload for make_heap? Is it the () operator?

  • 0

What operator do I have to overload for make_heap? Is it the () operator? If I already have that defined for another case in my algorithm. Can anyone provide the correct way to use make_heap in my case. Please see code below to better understand.

Inside a vertex class I have

bool operator() (vertex &v) const 
{ 
  return (v.key() == _key); 
}

This is used while constructing a graph in the following std method find_if

vertex_iterator GraphComponents::graph::find_vertex_by_key(int key)
{
  return std::find_if(_vertices.begin(), _vertices.end(), GraphComponents::vertex(key));
}

Now in my algorithm I want to use vertex as a Function object under a different context.

std::list<int> GraphComponents::graph::breadth_first_search (int key, int start_key)
{
  std::vector<GraphComponents::vertex *> heap;
  for (vertex_iterator copy_iter = _vertices.begin(); copy_iter != _vertices.end(); ++copy_iter) {
    heap.push_back(&(*copy_iter));
  }
  std::make_heap(heap.begin(), heap.end(), vertex(<should be distance>));
}

Here I don’t want to use the key in the comparison, but I want to use a distance member so the vertex that has the shortest distance is at the top of the heap. Short from implementing my own heap what is the recommended way around this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:05:10+00:00Added an answer on June 1, 2026 at 8:05 pm

    Implement a function which takes two arguments of your type, and returns true if the left hand argument should be considered relatively less than the right hand argument. (less can mean greater)

    Then pass that function as the third argument to make_heap. Or you can implement operator< using the semantics described above, and that will be used if you don’t pass any function.

    http://en.wikipedia.org/wiki/Strict_weak_ordering

    In your case, your elements are pointers, so you can’t write an operator<, as that function is already defined for all pointer types. So you will have to write a separate function, something like this:

    bool CompareByDistance(const GraphComponents::vertex * lhs,
                           const GraphComponents::vertex * rhs)
    {
        return lhs->distance() < rhs->distance();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something I can't understand. Let's say that I overload operator & to
Possible Duplicate: Overload ++ operator Say i have a class in which i overloads
I have a Card class and I want to overload the > operator to
Am using a XML sort of language which doesnt have '||' operator. How can
I have some code using Lucene that leaves the default conjunction operator as OR,
I am looking to overload, let's say, the addition operator and have it add
I am wondering if there is a way to overload operator[] for a non-class
I am working on an assignment that introduces me to operator overloading. I have
I have a simple class for which I want to overload operator as below
Can/should an overloaded operator have to be inlined to gain better efficiency (wrt time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.