如何使用 std::find 从向量的第一个元素中查找字符串<字符串对<字符串、字符串> >类型?

How to use std::find to find a string from the first elements of a vector< pair < string, string > > type?

本文关键字:字符串 lt gt 类型 查找 find std 何使用 向量 元素 第一个      更新时间:2023-10-16

我尝试了以下代码:

    auto it = find(v.begin(),v.end(), name, [](const pair<string, string> &a,const string b)
                           {
                               return a.first == b;
                           }); // name contains the string to find.

但这给了我编译错误。帮我找出我的错误。

您尝试调用的重载不存在。您想使用find_if:

auto it = std::find_if(v.begin(),v.end(), [&](const pair<string, string> &a)
{
    return a.first == name;
}); // name contains the string to find.