- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这里要疯了。我通过谷歌搜索找到了 1 个不错的示例,其中人们将 unordered_map 与枚举类和哈希函数一起使用,但没有任何运气。我设法找到的那些最终总是说“改用 map ”。
我正在尝试执行以下操作:
Enum class facing
是我的 Sprite 正在看的方向。
Enum class Action
是我的 Sprite 正在执行的操作。
Animation
是一个包含不同动画的类,我稍后会调用它。
容器应该是这样的:
map
图中可以有多个FACING作为key,pair中可以有多个ACTION。
例子:
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> 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 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> ]
最佳答案
这里
auto myList = bull.GetlistAnimation();
为 myList
推导的类型是std::unordered_map<.....>
,也就是说,它不是引用。并且无法创建拷贝,因为 map 包含 unique_ptr
秒。你的意思是
auto& myList = bull.GetlistAnimation();
或者在 C++14 中,
decltype(auto) myList = bull.GetlistAnimation();
关于C++ unordered_multimap 插入散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31207633/
我在练习 unordered_multimaps 时遇到了一个问题,一个 unordered_multimap 包含另一个 unordered_multimap。编译器抛出一个错误,说 c++ 标准不
我在这里要疯了。我通过谷歌搜索找到了 1 个不错的示例,其中人们将 unordered_map 与枚举类和哈希函数一起使用,但没有任何运气。我设法找到的那些最终总是说“改用 map ”。 我正在尝试执
我正在尝试使用 boost unordered_multimap 类,但在声明它时遇到了问题。错误跟在代码后面。 #include #include // header file needed f
一本书提到对于std::unordered_multimap: The order of the elements is undefined. The only guarantee is that d
所以,伙计们,我玩 std::unordered multimap 只是为了好玩。我想存储(在本例中)unsigned short,带有自定义哈希值和相等值。 有趣的部分是什么?如果两个项目都是偶数或
我尝试使用以下代码将一个值插入到 boost unordered_multimap 中,但这没有用,因为它无法编译。为什么没有访问 [] 运算符? insert()方法也不行? #include #
我有一个 std::unordered_multimap我想只对每个键进行一次迭代。 我目前正在做的是将所有 key 复制到 std::set .这对我来说似乎效率很低,我想知道是否有更聪明的方法来做
在初始化 boost::unordered_multimap 时,我们定义 HashMap 的大小。 explicit unordered_multimap(size_type n = impleme
我有一个 unordered_multimap 代表一个邻接表,还有几个有自环的边。例如: edges_ 可能是: edges_.insert(Edges::value_type(1, std::ma
我可以使用这段代码将所有元素放入一个桶中: typedef boost::unordered_multimap >
我希望 unordered_multimap::equal_range 具有平均恒定的复杂度,但是以下内容并不像预期的那样随 n 线性扩展: #include #include #include
我想创建一个映射,它使用迭代器作为键类型并使用整数作为值,如下例所示: #include #include int main(int argc, char* argv[]) { typedef
如果我有下面这段代码 std::unordered_multimap> myMap; std::vector v1, v2, v3; // init v1, v2, v3.... myMap.inse
我想构建一个 std::unordered_map,其中的值不是单个字符串、整数或 float ,而是元组、 vector 或结构。我知道这可以像@Vittorio_Romeo 和@CoryKrame
我在调试段错误时遇到问题。我很感激有关如何缩小问题范围的提示。 当迭代器试图访问结构 Infection 的元素时出现错误,定义为: struct Infection { public: expl
我有下面的代码,我知道我几乎是在重新实现容器,但我想这样做,我会有更具体的方法和我不想为不同类型的 multimap 重复代码: template class MapTemplate { public
我想访问/迭代 unordered_multimap 中的所有非唯一键。哈希表基本上是来自签名 的映射。这在实践中确实不止一次发生在标识符上 .我想在哈希表中找到那些出现一次的条目。 目前我使用这
在 boost unordered_multimap 中遍历唯一键的最简单方法是什么。 例如我有这个: std::set used; for (auto p : valuesMap) { if
当key不存在时,unordered_multimap::bucket(key)是什么应该回来吗? 引用资料说它应该返回包含键的桶的桶号,但没有说明当 unordered_multimap 中不存在该
我想知道 std::unordered_multimap 中关键对象的唯一性在处理迭代时。 我将尝试解释这一点:我需要将一些数据与 map 中的键类型相关联,这些数据不应在Hash 中考虑。或 Key
我是一名优秀的程序员,十分优秀!