我想从 map 中找到第一个非零元素,因此我做了以下代码:
#include <map>
#include <iostream>
#include <algorithm>
bool nonzero(std::map<char,int>::const_iterator& it);
int main() {
std::map<char, int> m;
m['a'] = 0;
m['b'] = 1;
std::map<char,int>::iterator it = std::find_if(m.begin(), m.end(), nonzero);
std::cout << it->first << '\t' << it->second << std::endl;
return 0;
}
bool nonzero(std::map<char,int>::const_iterator& it) {
return it->second;
}
g++ 给出的错误非常复杂,说:
/usr/include/c++/5/bits/predefined_ops.h:234:30: error: invalid initialization of reference of type ‘std::_Rb_tree_const_iterator<std::pair<const char, int> >&’ from expression of type ‘std::pair<const char, int>’
{ return bool(_M_pred(*__it)); }
我不明白它在说什么以及为什么我的程序会失败。
最佳答案
nonzero
的预期类型find_if
调用的函数不是 std::map<char,int>::const_iterator&
,而是一个 const std::pair<const char, int> &
.
事实上,如果你检查一些online documentation for find_if
,您会看到一元谓词具有以下形式:
bool UnaryPredicate(const Type&)
哪里Type
在你的情况下 std::pair<const char, int>
(对于一般的 std::map<Key, Value>
,类型是 std::pair<const Key, Value>
)。
所以你可以通过 const&
来调整你的函数那个std::pair
:
bool nonzero(const std::pair<const char, int> & p)
{
return (p.second != 0);
}
请注意,使用 C++14 auto
使用 lambdas 会简化您的代码,例如:
auto it = std::find_if(m.begin(), m.end(), [](const auto& p){
return (p.second != 0);
});
另请注意,该对具有一般形式 std::pair<const Key, Value>
(不仅仅是 pair<Key, Value>
和非常量键)。
关于c++ - 为什么像这样使用 find_if 会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40158556/