所以我使用多重集来解决我的编程问题
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/