c++ - 为什么像这样使用 find_if 会失败?

标签 c++ dictionary stl find stl-algorithm

我想从 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/

相关文章:

c# - 获取过去一年的 Facebook 数据?

C++:nepero 常量在某处是确定的吗?

c++ - open 的返回值 ("dev/null",O_APPEND) 可以表示打开的文件太多了吗?

python-3.x - 将字典的特定字典转换为 pandas 数据框 - pandas

c++ - 使用容器中元素的别名使用 std::list::remove 删除元素是否正确?

c++ - 初始化字符串 vector 数组

c++ - QPushButton 和点击插槽

python - 将文本文件中的不同数据添加到不同的词典中

c# - 比较两个字典并返回另一个字典中的差异

c++ - 可以安全地存储 list::iterator 供以后使用吗?