c++ - 如何在 boost::unordered_map 中实现 TryGetValue?

标签 c++ boost boost-unordered

C# 中,我喜欢 DictionaryTryGetValue 方法,因为它允许我在一次调用中确定字典是否包含键,如果包含则接收值:

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

我应该如何用 boost::unordered_map 做同样的事情?

最佳答案

使用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";
}

关于c++ - 如何在 boost::unordered_map 中实现 TryGetValue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16316217/

相关文章:

c++ - 使用虚拟继承时强制调用基本构造函数,尽管它永远不会被调用?

C++ 异常和 .eh_frame ELF 部分

c++ - boost spsc 队列段错误

boost - 安装 boost 库时找不到 user_config.jam

c++ - 动态 equal_to 函数 unordered_map boost

c++ - boost::unordered_multimap:有效地获取桶中的所有元素?

c++ - Eclipse Unresolved inclusion Boost

c++ - 减少调试 session 中的堆栈大小以捕获无休止的递归

c++ - 在 Windows 中无法从 cmake 中找到 MySQL 和 Boost includes/libs

c++ - boost::unordered_map 缺少像 std::unordered_map 这样的 reserve()