gpt4 book ai didi

c++ - 在 c++ 中的 map> 中查找 vector 中的值

转载 作者:太空宇宙 更新时间:2023-11-04 16:07:40 24 4
gpt4 key购买 nike

std::map< std::string, std::vector<std::string> > families;

// add new families
families["Jones"];
families["Smith"];
families["Doe"];

// add children
families["Jones"].push_back( "Jane" );
families["Jones"].push_back( "Jim" );

我使用上述方法将值添加到 map 中的 vector 。如何检查键为“Jones”的 map 中是否存在“Jane”?

最佳答案

为此使用 std::map 的 find 成员函数,然后进行常规字符串搜索:

std::map<std::string, std::vector<std::string> >::const_iterator search = families.find("Jones");
if(search != families.end())
{
std::vector<std::string> s = search->second;
if (std::find(s.begin(), s.end(), "Jane") != s.end())
{
}
}

关于c++ - 在 c++ 中的 map<string, <vector<string>> 中查找 vector 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32775839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com