c ++ unordered_multimap insert hash [英] c++ unordered_multimap insert hash
问题描述
我在这里疯了。我有搜索google找到1单一体面的例子,人们使用unordered_map与枚举类和散列函数没有任何运气。
我试图做以下事情:
面向枚举类
是我的精灵正在查看的方向。
枚举类Action
是我的sprite正在做的动作。
动画
是一个类,用于保存不同的动画,我稍后调用。
像这样:
map
地图中可以有超过1个FACING键,
例如:
map< ; LEFT,pair< ATTACK,attackAnimation>
map< LEFT,pair< IDLE,idleAnimation>
map< LEFTUP,pair< IDLE,idleAnimation>
这是一个简化的一切
#include< iostream>
#include< unordered_map>
#include< string>
#include< memory>
template< typename T>
struct Hash
{
typedef typename std :: underlying_type< T> :: type underlyingType;
typedef typename std :: hash< underlyingType> :: result_type resultType;
resultType operator()(const T& arg)const
{
std :: hash< underlyingType>哈希尔
return hasher(static_cast< underlyingType>(arg));
}
};
class动画
{
private:
std :: string str;
public:
动画(std :: string _string)
{
this-> str = _string;
}
std :: string& GetString()
{
return this-> str;
}
};
class Bullshit
{
public:
enum class Action
{
Attack,
Move
};
枚举类面向
{
右,
上,
左
};
Bullshit()
{
}
std :: unordered_multimap< Bullshit :: Facing,std :: pair& :Action,std :: unique_ptr< Animation>>,Hash< Bullshit :: Facing>>& GetlistAnimation()
{
return this-> listAnimation;
}
private:
std :: unordered_multimap< Bullshit :: Facing,std :: pair< Bullshit :: Action,std :: unique_ptr< Animation>>,Hash< ; Bullshit :: Facing>> listAnimation;
};
int main()
{
Bullshit bull;
auto myList = bull.GetlistAnimation();
std :: unique_ptr< Animation> anim(new Animation(test));
myList.insert(std :: make_pair(Bullshit :: Facing :: Up,std :: make_pair(Bullshit :: Action :: Attack,std :: move(anim))));
std :: cin.get();
return 0;
}
错误代码:
< blockquote>
错误C2248:'std :: unique_ptr< _Ty> :: unique_ptr':无法访问在'std :: unique_ptr< _Ty&
1> with
1> [
1> _Ty = Animation
1> ]
1> c:\program files(x86)\microsoft visual studio 11.0 \vc\include\ memory(1447):参见声明'std :: unique_ptr< _Ty> :: unique_ptr'
1& with
1> [
1> _Ty = Animation
1> ]
1>该诊断发生在编译器生成的函数'std :: pair< _Ty1,_Ty2> :: pair(const std :: pair< _Ty1,_Ty2>&)'
1& with
1> [
1> _Ty1 = Bullshit :: Action,
1> _Ty2 = std :: unique_ptr< Animation>
1> ]
/ p>
auto myList = bull.GetlistAnimation();
推导出 myList
的类型为 std :: unordered_map< .....>
,也就是说,它不是参考。并且无法创建副本,因为地图包含 unique_ptr
。你的意思是
auto& myList = bull.GetlistAnimation();
或在C ++ 14中,
decltype(auto)myList = bull.GetlistAnimation();
I'm going crazy over here. I have search google to find 1 single decent example where people use a unordered_map together with enum class and a hash function without any luck. Those i manage to find always end up saying "use map instead".
I'm trying to do the following:
Enum class facing
is the direction my sprite is looking at.
Enum class Action
is the action my sprite is doing.
Animation
is a class that holds different animations which i will call later.
The container should look like this:
map
There can be more than 1 FACING in the map as key and there can be more than one ACTION in the pair.
Example:
map<LEFT, pair<ATTACK, attackAnimation>
map<LEFT, pair<IDLE, idleAnimation>
map<LEFTUP, pair<IDLE, idleAnimation>
This is an simplified everything
#include <iostream>
#include <unordered_map>
#include <string>
#include <memory>
template <typename T>
struct Hash
{
typedef typename std::underlying_type<T>::type underlyingType;
typedef typename std::hash<underlyingType>::result_type resultType;
resultType operator()(const T& arg) const
{
std::hash<underlyingType> hasher;
return hasher(static_cast<underlyingType>(arg));
}
};
class Animation
{
private:
std::string str;
public:
Animation(std::string _string)
{
this->str = _string;
}
std::string& GetString()
{
return this->str;
}
};
class Bullshit
{
public:
enum class Action
{
Attack,
Move
};
enum class Facing
{
Right,
Up,
Left
};
Bullshit()
{
}
std::unordered_multimap<Bullshit::Facing, std::pair<Bullshit::Action, std::unique_ptr<Animation>>,Hash<Bullshit::Facing>>& GetlistAnimation()
{
return this->listAnimation;
}
private:
std::unordered_multimap<Bullshit::Facing, std::pair<Bullshit::Action, std::unique_ptr<Animation>>,Hash<Bullshit::Facing>> listAnimation;
};
int main()
{
Bullshit bull;
auto myList = bull.GetlistAnimation();
std::unique_ptr<Animation> anim(new Animation("test"));
myList.insert(std::make_pair(Bullshit::Facing::Up, std::make_pair(Bullshit::Action::Attack, std::move(anim))));
std::cin.get();
return 0;
}
Error code:
error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' 1> with 1> [ 1> _Ty=Animation 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(1447) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr' 1> with 1> [ 1> _Ty=Animation 1> ] 1> This diagnostic occurred in the compiler generated function 'std::pair<_Ty1,_Ty2>::pair(const std::pair<_Ty1,_Ty2> &)' 1> with 1> [ 1> _Ty1=Bullshit::Action, 1> _Ty2=std::unique_ptr<Animation> 1> ]
Here
auto myList = bull.GetlistAnimation();
the type deduced for myList
is std::unordered_map<.....>
, that is, it's not a reference. And the copy can't be created because the map contains unique_ptr
s. What you meant is
auto& myList = bull.GetlistAnimation();
or in C++14,
decltype(auto) myList = bull.GetlistAnimation();
这篇关于c ++ unordered_multimap insert hash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!