我正在尝试创建指向我的对象的指针数组的散列。
哈希键是对象类型的 int,数组是要呈现的对象列表。
我想要做的是:
unordered_map<int, vector<Object*> > drawQueue;
drawQueue.clear(); // new empty draw queue
for ( ... ) {
drawQueue.at(type).push_back(my_obj);
}
Run Code Online (Sandbox Code Playgroud)
所以我对 STL 内容的细微差别还不够熟悉,因为我收到一个异常,说 out_of_bounds,当密钥不存在时会发生这种情况。
所以我想我需要先创建密钥,然后添加到向量中:
if (drawQueue.count(type)) {
// key already exists
drawQueue.at(type).push_back(my_obj);
} else {
//key doesn't exist
drawQueue.insert(type, vector<Object*>); // problem here
drawQueue.at(type).push_back(my_obj);
}
Run Code Online (Sandbox Code Playgroud)
但是现在我真的迷路了,因为我不知道如何创建/初始化/vector插入unordered_map...
还是我这样做完全错误?
使用 string 和 int 键之间的性能是否存在显着差异(如果我正在进行随机查找)?在我的特定情况下,字符串键更方便,但有一点困难,我也可以使用 int 键进行设置。我只是想知道是否值得付出额外的努力。
应该std::unordered_map<int, int>比 std::map` 快吗?我不在乎顺序,只是快速查找,所以我认为我应该使用哈希表。但后来我想也许它会尝试额外散列我的密钥或类似的东西(我不需要)?
还有一个相关的问题:我需要int通过int键检索一个值。我应该使用unordered_map<int, int>or unordered_set<pair<int, int> >(在这种情况下我需要为我的配对正确实现哈希函数)?
我一直在使用 Visual Studio 2010 进行开发,然后在另一台机器上编译 Linux 64 版本。为了涵盖 2 个不同编译器/环境之间的差异,我们有条件包含语句:
#ifdef __linux__
#include <tr1/unordered_map>
#endif
#ifdef _WIN32
#include <unordered_map>
#endif
using namespace std; // covers std::unordered_map
using namespace std::tr1; // covers tr/unordered_map
unordered_map<string,string> map;
Run Code Online (Sandbox Code Playgroud)
对于unordered_map,我一直在使用此文档: cplusplus.com,它显示了at()在地图中查找键的方法。(与[]运算符不同,如果未找到,则不会将键插入到地图中。)
当我试图在 Linux 机器上编译代码时,gcc 抛出一个错误说
test_map.cpp:18: 错误: 'class std::tr1::unordered_map, std::allocator >, std::basic_string, std::allocator >, std::tr1::hash, std::allocator > >, std::equal_to, std::allocator > >, std::allocator, std::allocator >, std::basic_string, std::allocator > > >, false>'没有名为'at'的成员
本机gcc的版本是:
g++ (GCC) 4.1.2 20080704(红帽 4.1.2-46)
我尝试在较新的 …
在 boost unordered_multimap 中循环唯一键的最简单方法是什么。
例如我有这个:
std::set<int> used;
for (auto p : valuesMap)
{
if (used.count(p.first))
continue;
used.insert(p.first);
auto range = valuesMap.equal_range(p.first);
if (p.first)
for (auto v = range.first; v != range.second; ++v)
//do something;
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点。似乎无序映射应该已经知道唯一键。
我正在尝试使用自定义键创建一个映射,如上所述,该键是对象的指针地址。
我需要该地址,因为目前这是比较两个对象的唯一相关方法。
据我了解,执行此操作的正确方法是使用const char*as key
这里是typedef:
typedef __gnu_cxx::unordered_map<const char*, std::string> TargetsTags;
Run Code Online (Sandbox Code Playgroud)
我对以下内容有点困惑:
我如何创建operator()?
这就是我用的std::string:
namespace __gnu_cxx {
template<>
struct hash<std::string>
{
hash<const char*> h;
size_t operator()(const std::string &s) const
{
return h(s.c_str());
};
};
}
Run Code Online (Sandbox Code Playgroud)
关于什么const char*?
这是正确的做法吗?
我正在尝试创建以下 unordered_map:
std::unordered_map<CString, CString, std::function<size_t(const CString &data)>> usetResponse(100, [](const CString &data)
{
return std::hash<std::string>()((LPCSTR)data);
});
Run Code Online (Sandbox Code Playgroud)
我为 CString 提供了哈希函数,但编译器仍然返回错误:
error C2338: The C++ Standard doesn't provide a hash for this type.
error C2664: 'std::unordered_map<CString,CString,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const
_Kty,_Ty>>>::unordered_map(std::initializer_list<std::pair<const _Kty,_Ty>>,unsigned int,const std::hash<_Kty> &,const _Keyeq &,const std::allocator<std::pair<const _Kty,_Ty>> &)' : cannot convert argument 1 from 'std::unordered_map<CString,CString,std::function<size_t (const CString &)>,std::equal_to<_Kty>,std::allocator<std::pair<const
_Kty,_Ty>>>' to 'const std::unordered_map<CString,CString,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const
_Kty,_Ty>>> &'
Run Code Online (Sandbox Code Playgroud)
请告诉我我做错了什么?
实际应用中,有什么情况std::unordered_map必须用 来代替std::map?
我知道它们之间的区别,比如内部实现、搜索元素的时间复杂度等等。
但我实在找不到确实std::unordered_map可以替代的情况std::map。
std :: multimap和std :: unordered_multimap多久会有一次shuffle条目?我问,因为我的代码传递引用以区分具有相同哈希的条目,并且我想知道何时对它们运行引用重定向功能.
如果我这样做会发生什么:
std::multimap atable; //Type specification stuff left out
//Code that pus in two entries with the same key, call that key foo
int bar = atable[foo];
Run Code Online (Sandbox Code Playgroud)如果它是unordered_multimap,结果会有所不同吗?
返回传递引用以区分具有相同哈希的条目.有更安全的方法吗?
如果我删除其中一个条目,这些条目是否会移动(这是读取std :: vector文档的建议)?
我们知道std::unordered_map::bucket返回一个bucket是容器内部哈希表中的一个槽,根据键的哈希值为其分配元素.如何在返回桶中获取begin-iterator和end-iterator?换句话说,我可以bucket_count用来计算桶数,如何检测每个桶中的物品?