c++ - 确定对象是否在 std::set 中

标签 c++ stl set

我正在尝试确定对象是否已包含在 std::set 中。根据 msdn(和其他来源),set::find 函数应该返回 end() 如果它没有找到您要求的元素。

但是,当我实现如下代码时,set::find 返回垃圾 (0xbaadf00d)。

set<Cell*> cellSet;

Cell* cell = new Cell();    

if (cellSet.find(cell) == cellSet.end())
{
    ...
}

我是否正确使用了它?我在 Visual C++ 2005 中工作。

最佳答案

您发布的代码将始终执行 if 中的代码,并且 0xbaadf00d 实现的“one-past-the-end "标记。

关于c++ - 确定对象是否在 std::set 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/820859/

相关文章:

c++ - 在 lambda 中捕获完美转发的变量

C++映射真的很慢吗?

c++ - 通过线程实现游戏引擎确定性

c++ - 输出迭代器的 value_type

c++ - string::string 构造函数中的奇怪 "Bus error"

python - 我可以使用集合理解从更大的字典列表中创建字典列表吗?

python - 疯狂使用 python 集

c++ - C++ 中的字符串对象末尾是否有空字符?

c++ - 对于具有线性存储的容器,可以使用原始指针而不是具有 STL 算法的迭代器吗?

swift - `public private(set) var numberOfEdits = 0` 可以写成 `public(get) private(set) var numberOfEdits = 0` 吗?