C++ std::map 查找值而不是键
C++ std::map look for values but not keys
我有一个 std::map 将结构映射到字符串:
struct st
{
std::string name;
int age;
}
std::map<st, std::string> m_SoundStructList;
现在我想根据作为名称的字符串查看地图并获取结构。
std::string lName="Kate"
auto iter = m_SoundStructList.find(lName);
st lStruct=it->fisrt;
现在我只能在结构上应用查找,而不能在字符串上应用查找。
如有任何帮助,我们将不胜感激。
您可能必须遍历地图并手动使用匹配元素。
对于 C++11,它将是
for(const auto& p : m_SoundStructList) {
if (p.second.name == "Kate") {
// Do something
}
}
我有一个 std::map 将结构映射到字符串:
struct st
{
std::string name;
int age;
}
std::map<st, std::string> m_SoundStructList;
现在我想根据作为名称的字符串查看地图并获取结构。
std::string lName="Kate"
auto iter = m_SoundStructList.find(lName);
st lStruct=it->fisrt;
现在我只能在结构上应用查找,而不能在字符串上应用查找。
如有任何帮助,我们将不胜感激。
您可能必须遍历地图并手动使用匹配元素。
对于 C++11,它将是
for(const auto& p : m_SoundStructList) {
if (p.second.name == "Kate") {
// Do something
}
}