我在Ubuntu中使用g ++
g ++(Ubuntu 4.4.3-4ubuntu5)4.4.3
我有这个代码
#include<unordered_map>
using namespace std;
bool ifunique(char *s){
unordered_map<char,bool> h;
if(s== NULL){
return true;
}
while(*s){
if(h.find(*s) != h.end()){
return false;
}
h.insert(*s,true);
s++;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
当我编译使用
g++ mycode.cc
Run Code Online (Sandbox Code Playgroud)
我收到了错误
error: 'unordered_map' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我现在一直在编写一个图像处理算法,在某些时候我需要收集一些关于转换像素的统计信息,以便更深入地了解我应该遵循的进一步开发方向.我需要收集的信息格式如下:
key: RGB value
value: int
Run Code Online (Sandbox Code Playgroud)
我做了什么,是我打开转换后的图像并迭代它,保存我需要的值std::unordered_map,具有以下签名:
typedef std::unordered_map<boost::gil::rgb8_pixel_t, unsigned int> pixel_map_t;
Run Code Online (Sandbox Code Playgroud)
在循环中:
for(int y = 0; y < vi.height(); y++) {
SrcView::x_iterator dst_it = src.row_begin(y);
for(int x = 0; x < vi.width(); x++, hits++) {
diff_map.insert(std::make_pair(dst_it[x], /* some uint32 */));
}
Run Code Online (Sandbox Code Playgroud)
我还编写了一个自定义哈希函数(它是一个完美的哈希函数:256^2 x R + 256 x G +
B- 所以无论桶和哈希表的布局如何,冲突都应该是最小的(合理的扩展).
我注意到的是,插入速度非常慢! - 在达到第11次迭代后,插入速度降低约100倍.我发生了大量的碰撞!尽管图像中的重复颜色数量非常少.
之后,我想消除代码中的任何可能的错误,并开始unordered_map使用原始键类型(如int)对STL哈希函数进行基准测试.
该基准的代码是:
std::size_t hits = 0, colls = 0;
for(int y = 0; y < vi.height(); y++) {
SrcView::x_iterator …Run Code Online (Sandbox Code Playgroud) 我想为某个类型实现移动构造函数(没有复制构造函数),该类型需要是一个值类型boost::unordered_map.我们称之为这种类型Composite.
Composite 有以下签名:
struct Base
{
Base(..stuff, no default ctor) : initialization list {}
Base(Base&& other) : initialization list {}
}
struct Composite
{
Base member;
Composite(..stuff, no default ctor) : member(...) {}
Composite(Composite&& other) : member(other.member) {} // <---- I want to make sure this invokes the move ctor of Base
}
Run Code Online (Sandbox Code Playgroud)
我想写这个,所以boost::unordered_map< Key , Composite >不需要复制构造函数,只需使用移动构造函数.如果可能的话,我不想Base在移动构造函数的初始化列表中使用复制构造函数Composite.
这可能吗?
嗨我曾经有一个unordered_set来保存我的16个int数组,现在我需要再存储一个int作为它的存储桶.我想知道我是否可以将数组插入到我的unordered_set中,还是可以使用我以前使用的相同模板?
#include <unordered_set>
#include <array>
namespace std
{
template<typename T, size_t N>
struct hash<array<T, N> >
{
typedef array<T, N> argument_type;
typedef size_t result_type;
result_type operator()(const argument_type& a) const
{
hash<T> hasher;
result_type h = 0;
for (result_type i = 0; i < N; ++i)
{
h = h * 31 + hasher(a[i]);
}
return h;
}
};
}
std::unordered_set<std::array<int, 16> > closelist;
int main()
{
std::array<int, 16> sn = {1,2,3,4,5,6,0,8,9,10,11,12,13,14,7,15};
closelist.insert(sn);
}
Run Code Online (Sandbox Code Playgroud)
我可以改成它吗?
std::unordered_map<std::array<int, 16>,int > closelist;
int main() …Run Code Online (Sandbox Code Playgroud) 看起来像reserve/rehash函数只预先分配桶的数量,而不是要插入的元素(key,vlaue)对的内存.
有没有办法我们可以预先为元素分配内存,所以低延迟的应用程序不需要浪费时间在动态内存分配上.
使用值类型为引用C++ 11的unordered_map是否合法?
例如 std::unordered_map<std::string, MyClass&>
我已经设法使用VS2013进行编译但是我不确定它是否应该因为它导致一些奇怪的运行时错误.例如vector subscript out of range,在尝试erase元素时抛出.
一些谷歌搜索导致发现你不能有一个引用的向量,但我找不到任何有关unordered_map的内容.
进一步的实验表明,vector subscript out of range它与引用的unordered_map无关,因为它是我的代码中的一个错误.
如果我在两个无序容器中插入相同的(大小和值)元素,那么使用两个迭代器遍历容器总会在同一位置给出相同的元素吗?
如果是的话,可以使用(单个!)散列函数来打破这个决定论吗?
boost::hash 具有大多数内置类型(包括容器)的散列函数.
但正如boost::hash_range函数描述中所述,范围的哈希算法
对元素的顺序很敏感,因此将它与无序容器一起使用是不合适的
因而,没有boost::hash专业化std::unordered_map,也没有boost::unordered_map.
问题是:
如果unordered_map没有从头开始重新实现哈希算法,是否有"简单而有效"的方法来哈希?
我想创建unordered_map(因为我特别想要一个哈希映射).我想在开头分配它的最大尺寸(根据我的约束).
所以,如果我想分配256个条目,每个条目的大小是1B(只是一个例子.假设1Byte包括Key和Value).然后我的unordered_map键+条目的总大小是256B.我想在分配器中预先分配256B.
然后,当unordered_map将调用allocate()/时deallocate(),allocator将从已分配的内存中给它1B.
typedef boost::unordered::unordered_map<int, MyClass, boost::hash<int>, std::equal_to<MyClass>, ??? > > myMap
它是否存在于BOOST中?或者别的地方?
----编辑----
正如我所看到的(感谢此处的答案) - 我的问题有两个解决方案:
实现一个allocator,持有一个boost::pool<>.这pool是在allocator构造函数中构建的.当allocate()被调用时unordered_map,它实际上调用pool.malloc(),并且当deallocate()调用unordered_map它时,它实际调用pool.free().
使用已经实现的allocator,pool_allocator如下所示:
typedef pool_allocator<std::pair<MyKey, MyClass>, boost::default_user_allocator_new_delete, boost::mutex, 1024 >) MyAllocator;
typedef unordered_map<MyKey, MyClass, hash, eq, MyAllocator> MyUnorderedMap;
秒选项对我来说仍然不清楚,因为:
a.我只能声明一个MyUnorderedMap吗?
湾 我怎样才能申报使用不同的新MyUnorderedMap next_block大小比1024在运行时间?
我正在尝试在游戏引擎项目上实现基于组件的架构.每个GameObject都有unordered_map一个指向Component基类的指针.此时,我只有一个组件派生类,即Transform类.我想实现这个基于组件的体系结构,类似于Unity的约定:我想通过调用成员模板函数来获取游戏对象的一个组件GetComponent<Transform>().
以下是标题:
Component.h
enum type{
TRANSFORM // more will be added later
};
class Component // base class
{
public:
Component() : _owner(NULL) {}
virtual ~Component(){}
static type Type;
protected:
GameObject* _owner;
};
Run Code Online (Sandbox Code Playgroud)
Transform.h
class Transform : public Component
{
public:
Transform();
~Transform();
static type Type;
void Rotate(float deg);
// to be encapsulated later on
Vector2D _position;
float _rotation;
Vector2D _scale;
};
Run Code Online (Sandbox Code Playgroud)
GameObject.h
class GameObject
{
public:
GameObject();
~GameObject();
void Update();
//and …Run Code Online (Sandbox Code Playgroud) c++ ×10
unordered-map ×10
c++11 ×5
hashtable ×3
boost ×2
stl ×2
allocator ×1
hash ×1
inheritance ×1
performance ×1
reference ×1
templates ×1