我跟踪了一个错误,即使用__m128(SSE向量)作为std :: unordered_map中的值.这会导致mingw32 g ++ 4.7.2的运行时分段错误.
请参阅下面的示例.这有什么理由会失败吗?或者,可能有一个解决方法?(我尝试将值包装在一个类中,但它没有帮助.)谢谢.
#include <unordered_map>
#include <xmmintrin.h> // __m128
#include <iostream>
int main()
{
std::unordered_map<int,__m128> m;
std::cerr << "still ok\n";
m[0] = __m128();
std::cerr << "crash in previous statement\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译设置:g ++ -march = native -std = c ++ 11
我正在试图找出为资源进行缓存的最佳方法.我主要是寻找原生的C/C++/C++ 11解决方案(即我没有提升和喜欢的选项).
从缓存中检索时我正在做的是这样的:
Object *ResourceManager::object_named(const char *name) {
if (_object_cache.find(name) == _object_cache.end()) {
_object_cache[name] = new Object();
}
return _object_cache[name];
}
Run Code Online (Sandbox Code Playgroud)
在哪里_object_cache定义如下:std::unordered_map <std::string, Object *> _object_cache;
我想知道的是关于这样做的时间复杂性,是否找到触发线性时间搜索或者它是否作为某种查找操作完成?
我的意思是如果我_object_cache["something"];在给定的例子上做它将返回对象或如果它不存在它将调用默认构造函数插入一个不是我想要的对象.我觉得这有点违反直觉的,我本来期望它以某种方式来报告(返回nullptr为例),一个value为key无法检索,而不是第二个猜测我想要的东西.
但是,再次,如果我find在键上执行操作,是否会触发一个大搜索,实际上它将以线性时间运行(因为找不到键会查看每个键)?
这是一个很好的方法吗,或者是否有人有一些建议,也许有可能使用查找或某些东西来知道密钥是否可用,我可以经常访问,如果是这样的话,有时候是花在搜索上我想消除它,或者至少尽可能快地消除它.
感谢任何关于此的输入.
我想知道为什么C++ 11和Boost的hashmap在通过迭代擦除元素时不会调整大小.即使这在技术上不是内存泄漏,我认为它可能是应用程序中的一个严重问题(对我来说这是一个隐藏的问题,很难追溯它)并且它实际上可能影响许多应用程序.这是容器的"设计缺陷"吗?
我对它进行了基准测试,似乎影响了几个编译器版本(包括VS,Clang,GCC)
重现问题的代码是:
std::unordered_map<T1,T2> m;
for (int i = 0; i < 5000000; i++)
m.insert(std::make_pair(i, new data_type));
for (map_type::iterator it = m.begin(); it != m.end();) {
delete it->second;
it = m.erase(it);
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个自包含的测试文件,它使用自定义分配器来跟踪内存使用情况.
只要我理解,其背后的原因是允许通过迭代擦除元素并保持有效的迭代器不被擦除的元素.这似乎有点奇怪的要求,因为插入元素可能导致重新哈希无论如何使迭代器无效.
但你可以直接破坏地图..
我是如何解决这个问题的(我将地图包裹在一个智能指针内,当它是空的时我只是重新创建一个新的空地图,结果比重新加速更快,不知道为什么.).
通常,任何unordered_map用作缓存元素容器的应用程序都可能遇到此问题(您可能希望从缓存中删除元素,但通常没有人执行"缓存重置")
在阅读用于std :: unordered_map的std :: hash示例时,我注意到{}正在访问operator()函数.
http://en.cppreference.com/w/cpp/utility/hash
result_type operator()(argument_type const& s) const
{
result_type const h1 ( std::hash<std::string>{}(s.first_name) );
result_type const h2 ( std::hash<std::string>{}(s.last_name) );
return h1 ^ (h2 << 1); // or use boost::hash_combine (see Discussion)
}
Run Code Online (Sandbox Code Playgroud)
这里使用{}代表什么?
I have C++ code that investigates a BIG string and matches lots of substrings. As much as possible, I avoid constructing std::strings, by encoding substrings like this:
char* buffer, size_t bufferSize
Run Code Online (Sandbox Code Playgroud)
At some point, however, I'd like to look up a substring in one of these:
std::unordered_map<std::string, Info> stringToInfo = {...
Run Code Online (Sandbox Code Playgroud)
So, to do that, I go:
stringToInfo.find(std::string(buffer, bufferSize))
Run Code Online (Sandbox Code Playgroud)
That constructs a std::string for the sole purpose of the lookup.
I feel like there's an optimization I could do here, …
如果我有 unordered_map<key, someNiceObject>
(注意someNiceObject不是指针)
我有一个API,它插入一个新元素,然后someNiceObject在地图中返回一个指向现在的指针.
如果我在地图中执行进一步插入,则可能会有容量更改.如果发生这种情况,指针仍然有效?
我尝试阅读 基本问题:指向unordered_maps(C++)中的对象, std :: unordered_map指针/引用失效和 http://eel.is/c++draft/unord.req#9
并找不到必要的信息
谢谢大家
虽然会感谢来自这里的某人的第二次确认.
我正在尝试创建指向我的对象的指针数组的散列。
哈希键是对象类型的 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...
还是我这样做完全错误?
应该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)
有没有更好的方法来做到这一点。似乎无序映射应该已经知道唯一键。