2014-02-20 76 views
0

我想一个元件多重映射一个字符串比较,如:比较多重映射与字符串

struct value { 
string res; 
time_t t; 
}; 

string result; 

multimap<int, value> values 
value new_value; 

if((values.find(new_value.res)) != result) // code snipped 
{ 
//... do something 
} 

谢谢!

+0

什么你的代码有错?你有尝试过什么吗? – rockinfresh

回答

0

您可以使用std::find和lambda表达式

auto it=std::find_if(values.begin(), values.end(), [&](const std::pair<int,value> &pair) 
{ 
    return pair.second.res == new_value.res 
}); 

if (it != values.end()) 
{ 
    // Found it 
} 

如果您没有访问到C++,然后11,你可以遍历它:

for(std::map<int,value>::iterator it=values.begin(); it!=values.end(); ++it) 
{ 
    if((*it).second.res == new_value.res) 
    { 
    // Found it, 
    break; 
    } 
} 
+1

我想你的意思是'std :: find_if'? 'std :: find'函数将* value *作为第三个参数。 –

+0

@JoachimPileborg - 谢谢 – Sean

0

您不能使用std::multimap::find函数,因为它只搜索密钥。

相反,您必须使用更通用的std::find_if函数,并使用自定义谓词。喜欢的东西

std::find_if(std::begin(values), std::end(values), 
    [&new_value](const std::pair<const int, value> &p) 
    { 
     return p.second == new_value.res; 
    }); 
+0

我认为你的意思是'const std :: pair Sean

+1

@Sean你是对的。不知道我怎么会误解这个。 :) –