map_data
您忘记了定义后的分号。修复该问题,C++11 中的编译成功:
#include <map>
using std::map;
struct map_data{
int data1;
int data2;
};
struct
{
bool operator()(map_data one, map_data two) const
{
if(one.data1 == two.data1)
return one.data2 > two.data2;
else
return one.data1 < two.data1;
}
}compare;
int main() {
map<map_data, int, decltype(compare)> the_map;
}
但是,为此需要 C++11 并在您真正需要的只是类型时decltype
实例化一个对象似乎有点浪费。compare
为什么不传统呢?
#include <map>
using std::map;
struct map_data {
int data1;
int data2;
};
struct map_data_comparator
{
bool operator()(const map_data& one, const map_data& two) const
{
if (one.data1 == two.data1)
return one.data2 > two.data2;
else
return one.data1 < two.data1;
}
};
int main()
{
map<map_data, int, map_data_comparator> the_map;
}
You can see that I've also made your comparator arguments const references, just for the sake of it really.