Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing less than operator for one element of a pair

What would be the most elegant way too fix the following code:

#include <vector>
#include <map>
#include <set>
using namespace std;

typedef map< int, int > row_t;
typedef vector< row_t > board_t;
typedef row_t::iterator area_t;

bool operator< ( area_t const& a, area_t const& b ) {
    return( a->first < b->first );
};

int main( int argc, char* argv[] )
{
    int row_num;
    area_t it;

    set< pair< int, area_t > > queue;
    queue.insert( make_pair( row_num, it ) ); // does not compile
};

One way to fix it is moving the definition of less< to namespace std (I know,  you are not supposed to do it.)

namespace std {
    bool operator< ( area_t const& a, area_t const& b ) {
        return( a->first < b->first );
    };
};

Another obvious solution is defining less than< for pair< int, area_t > but I'd like to avoid that and be able to define the operator only for the one element of the pair where it is not defined.

like image 372
Koszalek Opalek Avatar asked Feb 24 '10 19:02

Koszalek Opalek


People also ask

How do you assign a value to a pair in C++?

Syntax: This assigns pr to the pair object's new text. The first value receives the first value of pr, while the second receives the second value of pr. With C++ pair, use the comparison (==) operator: The comparison operator measures the first and second values of two sets, say pair1 and pair2, to see whether pair1.

How do you get the second element in pair?

To access the elements of the pair, use variable name followed by dot operator followed by the keyword 'first' or 'second', these are public members of class pair.

How do you check if two pairs are equal in C++?

The equality ( == ) operator returns true if two pairs contain the same values. The inequality ( != ) operator returns true if two pairs do not contain the same values.

How do you create an array of pairs in C++?

The first element of pair Arr1 is sorted with the pair elements of pair “Arr2”. In the main function, we have initialized the values for pair array “Arr1” and pair array “Arr2”. These sorted arrays and the original pairs array will be displayed by using the cout command.


1 Answers

When you are implementing a comparator that implements some specific and/or fairly exotic comparison approach, it is better to use a named function or a function object instead of hijacking the operator < for that purpose. I'd say that the natural way to compare a std::pair object would be to use lexicographical comparison. Since your comparison is not lexicographical, taking over operator < might not be a good idea. Better implement a comparator class

typedef pair< int, area_t > Pair; // give it a more meaningful name

struct CompareFirstThroughSecond {
  bool operator ()(const Pair& p1, const Pair& p2) const { 
    if (p1.first != p2.first) return p1.first < p2.first;
    return p1.second->first < p2.second->first;
  }
};

and use it with your container

std::set< Pair, CompareFirstThroughSecond > queue;  

(I hope I deciphered your intent from your original code correctly).

You can also implement the above operator () method as a template method, thus making it usable with all std::pair-based types with an iterator as a second member. It might not make mich sense though, since your comparison is "exotic" enough.

like image 104
AnT Avatar answered Oct 24 '22 15:10

AnT