• 您的位置我爱Aspx >> VC.Net >> Effective STL: Item 21:永远让比较函数对相同元素返回false
  • Effective STL: Item 21:永远让比较函数对相同元素返回false

  • 作者:aspxer  来源:internet  日期:2007-5-21 23:48:06  关键字:
  • const string*, // from page 89. Beware,

    bool> { // this code is flawed!

    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

    const string*, // associative containers

    bool> {

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

    {

    return *ps2 < *ps1; // return whether *ps2

    我对这篇文章有话说?
  • 广告位招租,广告代号:content_468_15
  • 上一篇:Policy Tool — 策略文件创建和管理工具
    下一篇:获取系统当前打开的端口(tcp。udp)状态,以及连接方的ip。端口
  • 相关文章