Blu*_*jay 1 c++ lambda stdmap find c++11
我有一张地图如下:
std::map<int, std::unique_ptr<Person>> ratingMap;
Run Code Online (Sandbox Code Playgroud)
我想创建一个函数,它接受一个字符串参数_name并遍历地图,直到它找到一个具有相同名称的人:
void Person::deleteFromMap(const std::string& _name){
//Searches the map for a person whose name is the same as the argument _name
auto found = std::find(ratingMap.begin(), ratingMap.end(),
[&](const std::unique_ptr<Person>& person) -> bool{return person->getName() == _name; });
Run Code Online (Sandbox Code Playgroud)
但是,这拒绝编译并给出以下错误:
错误1错误C2678:二进制'==':找不到哪个运算符带有'std :: pair'类型的左操作数(或者没有可接受的转换)
我已经花了将近两个小时尝试其中的变体以试图让它工作,因为我在过去编写了类似的lambda函数,这些函数已按预期编译和工作.为什么会这样?
它应该是
void Person::deleteFromMap(const std::string& _name){
//Searches the map for a person whose name is the same as the argument _name
auto found = std::find_if(ratingMap.begin(), ratingMap.end(),
[&](std::pair<const int, std::unique_ptr<Person>>& p) -> bool{return p.second->getName() == _name; });
Run Code Online (Sandbox Code Playgroud)
为map::value_type是std::pair<const int, std::unique_ptr<Person>>.
编辑:正如其他人所指出的那样,它std::find_if取决于谓词.
| 归档时间: |
|
| 查看次数: |
979 次 |
| 最近记录: |