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

  • Home
  • SEARCH
  • 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 3280000
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:39:20+00:00 2026-05-17T19:39:20+00:00

I’m having troubles in overloading comparison operators in order to compare two pair struct

  • 0

I’m having troubles in overloading comparison operators in order to compare two pair struct in such way:

typedef pair<string, unsigned int> INDEX;
bool operator>(INDEX &v1, INDEX &v2)
{
    if(v1.second == v2.second)  //if integer parts are equal
    {
        //string that comes earlier in the dictionary should be larger
        return v1.first < v2.first; 
    }
    return v1.second > v2.second;
}

The actual comparison takes place at this->element(hole/2) < this->element(hole) inside fixUp(CBTNODE hole), a member function of BinaryHeap class, which is a derived class of CompleteBinaryTree. The T will be instantiated as type INDEX, which is typedefed as pair<string, unsigned int>.

In other words, the comparison between two pairs: (“a.txt”, 42) > (“b.txt”, 42) should return true.

I tried to overload operator> outside the class declaration in two different ways but neither of them worked:

  1. bool operator>(INDEX &v1, INDEX &v2);
  2. bool operator>(BinaryHeap<T> &v1, BinaryHeap<T> &v2);

Any help will be much appreciated!

Z.Zen

Here is the declarations:

typedef int CBTNODE;

template <typename T>
class CompleteBinaryTree {
public:
  //Initializes an empty binary tree
  CompleteBinaryTree(int initialSize = 10);

  //Destructor
  ~CompleteBinaryTree();

  //Returns the element of the CBT pointed to by node. Behavior is undefined
  //if node does not exist. 
  T element(CBTNODE node);

protected:
  T *data;
  int numElts, maxElts;
};

typedef pair<string, unsigned int> INDEX;

template <typename T>
class BinaryHeap : public CompleteBinaryTree<T>
{
    public:
        //Maintain heap property with bottom up heapify method.
        void fixUp(CBTNODE hole);
};
bool operator>(INDEX &v1, INDEX &v2);

Implementation:

template <typename T>
T CompleteBinaryTree<T>::element(CBTNODE node) {
  assert(node >= 0);
  assert(node < numElts);
  return data[node];
}

template <typename T>
void BinaryHeap<T>::fixUp(CBTNODE hole)
{
    T tmp = this->element(hole);    
    while( hole > 0 && this->element(hole/2) < tmp )
    {
        //do stuff
    }
}

bool operator>(INDEX &v1, INDEX &v2)
{
    if(v1.second == v2.second)  //if two have same relevance
    {
        return v1.first < v2.first;
    }
    return v1.second > v2.second;
}
  • 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-05-17T19:39:20+00:00Added an answer on May 17, 2026 at 7:39 pm

    A temporary, such as the result of element func, cannot be bound to a reference to non-const, such as the formal arguments of your operator>.

    Declare it thusly:

    bool operator>( INDEX const& v1, INDEX const& v2 )
    

    However, the implementation that you present doesn’t seem to be correct for operator>.

    And while I’m at it, what you want is really operator< instead, because that’s the one required by standard algorithms. Perhaps combined with an operator== (because it’s inefficient to synthesize it from operator<). With those two any relationship can be checked for relatively efficiently.

    Btw., if you stop using ALL UPPERCASE names for anything else then macros (see the FAQ), then you can avoid inadvertent name collision with macros.

    Cheers & hth.,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.