如何在boost::unordered_map中实现TryGetValue
how to implement TryGetValue in boost::unordered_map?
在C#
中,我喜欢Dictionary
的TryGetValue
方法,因为它允许我在一次调用中确定字典是否包含键和接收值,如果是的话:
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 foundn";
}
相关文章:
- 在C++中实现类似 python "map"函数的问题:调用类成员函数
- 在libc++和libstdc++之间的std::map上使用std::find时的实现差异
- libc++ 对 std::map/set::equal_range 的实现给出了意想不到的结果
- 如何实现 std::map::查找包含两个结构'Pos'结构的比较逻辑,每个结构包含 x 和 y 坐标
- 使用值类在C 中实现STL MAP
- 为什么 vector::operator[] 的实现方式与 map::operator[] 不同?
- std::与VS2013相比,GCC 4.7.2中的MAP实现效率非常低
- std::map 的排序是如何实现的
- std::map 是如何实现的,以便它可以要求其key_type具有可比性
- 用预定义的大小在C++中实现Map
- 尝试使用map实现
- 为什么C++STL使用RBtree来实现"std::map"?
- 在C++11中从C++17重新实现std::map::try_emplace()
- STL::MAP在C++中的内部实现
- 如何根据插入对的存在来实现不同的“std::map insert()”行为
- 下面使用Map和Vector实现递归级阶遍历的时间复杂度是多少
- 为什么python的字典是作为哈希表实现的,而std::map是基于树的?
- 自定义分配器- Microsoft std::map实现对相同的元素进行两次重新分配,GCC工作得很好
- 如何实现map<int,列表<string>>迭代器C++?
- 为什么libc++的map实现使用这种联合?