使用set构造bug
使用set构造BUG
原理:std::set的insert相同obj只会保留一个
- 当我们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等于 }
- 重载比较字符串的函数也可能产生bug
比如a="hello",b="helloworld"
,如果只比较前5个char,也会导致set里a或b有一个insert不进去