Search code examples
c++multimap

Compare multimap with String


I want to compare an element from a multimap with a string, like:

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
}

Thanks!


Solution

  • You can use std::find and a lambda expression

    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
    }
    

    If you don't have access to C++11 then you can loop over it:

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