我有一个字符串向量,我想从类似于字符串的向量中返回一个字符串。例如,向量包含:“load”,“fox”,“google”,“firefox”,字符串是:“mozilla firefox”。此示例中的真实结果是“firefox”。在C++中查找向量中的字符串
我使用下面的代码,但它是错误的,并返回我的示例“狐狸”。
vector<string>::const_iterator it_found = find_if(MyVector.begin(), MyVector.end(), [&MyString](string s) -> bool
{ return(MyString.find(s) != string::npos); });
if(it_found != MyVector.end())
{
//Do Somthing
}
我该怎么办?
您只检查'vector中的字符串'是否为搜索字符串的一部分。所以你的条件不够具体 – Zaiborg
“'fox”'和''firefox“'在''mozilla firefox”'中。你需要额外的标准来选择'“firefox”'而忽略'“fox”'。 –