Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement TryGetValue in boost::unordered_map?

In C# i like TryGetValue method of Dictionary because it allows me in one call determine if dictionary contains key and receive value if so:

Instrument instrument;
if (isinId2Instrument.TryGetValue(isin_id, out instrument))
{
    // key exist, instrument contains value
} else {
    // key doesn't exist
}

How should I do the same thing with boost::unordered_map?

like image 573
Oleg Vazhnev Avatar asked Mar 24 '23 03:03

Oleg Vazhnev


1 Answers

Use boost::unordered_map::find():

boost::unordered_map<std::string, int>::iterator i = m.find("hello");
if (i != m.end())
{
    std::cout << i->first << "=" << i->second << "\n";
}
else
{
    std::cout << "Not found\n";
}
like image 82
hmjd Avatar answered Apr 02 '23 01:04

hmjd