修复以下代码的最优雅方法是什么:
#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
};
解决它的一种方法是将 less< 的定义移动到命名空间 std (我知道, 你不应该这样做。)
namespace std {
bool operator< ( area_t const& a, area_t const& b ) {
return( a->first < b->first );
};
};
另一个明显的解决方案是为 pair< int, area_t > 定义小于<,但我想避免这种情况,并且只能为未定义的对中的一个元素定义运算符。