java - 集合如何区分对象?

标签 java c++ set

集合如何区分 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/

相关文章:

java - 为什么 Java HashMap get(key) 在使用相同的 HashMaps 迭代器读取键时比使用 Set 的迭代器读取键时工作得更快?

java - 在回调中上传两个文件到 firebase 但无法获取这两个文件的DownloadUrl

c++ - 是否有一些 escaped_list_separator 反向函数?

python - 从 python 列表中删除重复(非精确)元组

c++ - OpenMP:循环遍历 'std::map' 基准(动态调度)

c++ - 返回一个 char 数组而不是 const char *

Java 8 哈希集错误

java - 按钮无法转换为节点,方法列表add(node)不适用

java - 禁用 Spring Boot Web 客户端日志

Java 同步块(synchronized block)和匿名类