使用set构造bug

使用set构造BUG

原理:std::set的insert相同obj只会保留一个

  1. 当我们setA.insert(1),setA.insert(2),setA.insert(3)后,会发现setA里的1,2分别只有1个,
    故可以使用重载对象的operator<来让一个对象可能无法insert
    展开查看:重载<号的伪代码 class Obj { int id; operator<(other) { return this->id < other.id%2; } // 不需要要operator等于 }
  2. 重载比较字符串的函数也可能产生bug
    比如a="hello",b="helloworld",如果只比较前5个char,也会导致set里a或b有一个insert不进去
posted @ 2023-05-07 10:36  马角的逆袭  阅读(16)  评论(0编辑  收藏  举报