集合如何区分 Java 和 C++ 中的对象?还是集合根本无法区分它们?
以这些为例:
C++
std::set<A> aset;
A a(1, 2); // Assume A has only two elements, and this constructor sets them both
aset.insert(a);
A a2(1, 2); // This would initialise a `A' object to the same values as `a', but a different object
aset.count(a2); // Would this return 1 or 0?
Java
set<A> aset;
A a = new A(1, 2); // Assume A has only two elements, and this constructor sets them both
aset.add(a);
A a2 = new A(1, 2); // This would initialise a `A' object to the same values as `a', but a different object
aset.contains(a2); // Would this return true or false?
最佳答案
在 C++ 中,该集合取决于为类 A 定义的运算符 <(),或者您提供一个提供 strict weak ordering 的比较对象。到集合。
关于java - 集合如何区分对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6197536/