Providing less than operator for one element of a pair
Posted
by Koszalek Opalek
on Stack Overflow
See other posts from Stack Overflow
or by Koszalek Opalek
Published on 2010-02-24T19:35:05Z
Indexed on
2010/04/10
18:43 UTC
Read the original article
Hit count: 264
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.
© Stack Overflow or respective owner