std greater on a an std pair of a double and a class

0 votes

When you have a std::pair of int and a class, does std::greater work?

I'm attempting to construct a priority queue of pairs that is ordered by the first element:

std::priority_queue<std::pair<double, classA>, std::vector<std::pair<double, classA>>, std::greater<std::pair<double, classA>>> priorityQueue

But I get an error message that says

"operator" has no match.

It also refers to the class type of the second element of the std::pair.

Is std::greater applied to the first and second std::pair elements?

Jun 2 in C++ by Nicholas
• 2,060 points
12 views

1 answer to this question.

0 votes
std::greater is simply a wrapper for a template type operator call.

We can look at the reference site for std::pair and see that it says

Compares lhs and rhs lexicographically by operator, that is, compares the first elements and only compares the second elements if they are equivalent.

As a result, it employs the operator of both types, implying that your class type must provide it.

Since it doesn't you get the compiler error.
answered Jun 2 by Damon
• 3,380 points

Related Questions In C++

0 votes
1 answer

Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { ...READ MORE

answered Jun 1 in C++ by Damon
• 3,380 points
24 views
0 votes
0 answers

Intersection of two std::unordered_map

I have two std::unordered_map std::unordered_map<int, int> mp1; std::unordered_map<int, int> ...READ MORE

May 31 in C++ by Nicholas
• 2,060 points
26 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1 in C++ by Damon
• 3,380 points
15 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1 in C++ by Damon
• 3,380 points
15 views
0 votes
1 answer

How to expose std::pair to python using boost::python?

The most simple example of exposing std::pair is: class_<std::pair<int, int> ...READ MORE

answered Jun 20, 2019 in Python by SDeb
• 13,300 points
573 views
0 votes
1 answer

How to find out if an item is present in an std::vector?

The most straightforward solution is to count the total number of elements in the vector that have the specified value.  If the count is greater than zero, we've found our element.  This is simple to accomplish with the std::count function. #include <iostream> #include <vector> #include <algorithm> int main() { ...READ MORE

answered May 27 in Others by Damon
• 3,380 points
10 views
0 votes
1 answer

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

answered May 31 in C++ by Damon
• 3,380 points
11 views
0 votes
1 answer

What data structure is inside std::map in C++?

An associative container is std::map. The standard's ...READ MORE

answered May 31 in C++ by Damon
• 3,380 points
17 views
0 votes
1 answer

What is a Class and Object in C++?

A Class is like a blueprint, an ...READ MORE

answered 5 days ago in C++ by Damon
• 3,380 points
12 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1 in C++ by Damon
• 3,380 points
24 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP