c++ - 多重集 STL 无法识别的查找函数

标签 c++ stl

所以我使用多重集来解决我的编程问题

multiset<pair<int,int> > M;
multiset<pair<int,int> >::iterator it,it2;

并使用

向其中插入值
M.insert(make_pair(temp,ind));

但是现在,当我使用 find 函数时,要找到一个存在的值,比如说

it2=M.find(temp);

其中 temp 是一个整数,它会抛出这个错误:

F:\ABC.cpp||In function 'int main()':|
F:\ABC.cpp|42|error: no matching function for call to
'std::multiset<std::pair<int, int> >::find(int&)'|
F:\ABC.cpp|42|note: candidates are:|

我花了几天时间尝试调试它,但无济于事!任何人都可以解释为什么会发生这种情况,并提出补救措施吗?

附言我希望找到在多重集中只使用一个键值! (感谢@Nabla 的建议)

最佳答案

由于您希望容器按两个值排序,但只按第一个值搜索,这里有一个建议。

it = M.lower_bound(pair<int,int>(temp, numeric_limits<int>::min()));

这需要您 #include <limits> .它将返回一个迭代器,指向等于或高于 temp 的最小对的最低元素。作为第一个元素。然后,您可以遍历该集合,直到找到所有带有 temp 的对。作为第一个元素:

while((it != M.end()) && (it->first == temp)) {
     // Do something with the found element in it
     it++;
}

关于c++ - 多重集 STL 无法识别的查找函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21808613/

相关文章:

c++ - 支持实时和非实时模式的程序

c++ - 无法在 C++ 项目上从 Meson 运行 Doxygen

c++ - 将模板化基类的子类添加到没有超基类的容器中?

visual-c++ - MSVC++ map<int, list<unique_ptr<test>>> 不编译引用已删除的函数

c++ - 为什么STL的Map中的值没有变化?

c++ - 替换 Visual Studio 标准库

c++ - 如何动态跟踪 OpenGL 中的纹理单元?

c++ - VTK:如何为柏拉图式立体着色

c++ - 从类级放置新重载中访问成员

c++ - 我可以依靠短路评估来检查 C++ 中的 vector 边界吗?