Effective STL: Item 21:永远让比较函数对相同元素返回false[4]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 taodm 的 blog

bool operator()(const string *ps1, const string *ps2) const

{

return !( *ps1 < *ps2);                      // just negate the old test;

}                                                // this is incorrect!

};

想法是通过将比较函数内部结果取反来反序。很不幸,取反“<”不会给你(你所期望的)“>”,它给你的是“>=”。而你现在知道,因为“>=”将对相同的元素返回true,对关联容器,它不是一个有效的比较函数。

你真正需要的比较类型是这个:

struct stringptrgreater:                             // this is a valid

public binary_function<const string*,            // comparison type for

本文关键:Effective STL,Scott Meyers
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top