正在尝试将对象放置到无序映射中
Trying to place objects into unordered_map
我正在尝试创建一个以整数为键、以Transition对象为值的无序映射。。。。这是我的:
实例化
unordered_map<int, Transition> transitions;
过渡级声明:
class Transition{
public:
Transition(int n, char r, char m);
~Transition();
int getNextState();
char getReplacement();
char getMovement();
private:
int nextState;
char replacement;
char movement;
};
向地图添加转换
// Create transition object
Transition t(r,b,x);
transitions[keyForMap] = t;
我得到这个错误:
/usr/include/c++/4.7/bits/hashtable_policy.h:445:24: error: no matching function for call to ‘Transition::Transition()’
/usr/include/c++/4.7/bits/hashtable_policy.h:445:24: note: candidates are:
In file included from ball_p1.cpp:6:0:
Transition.h:4:3: note: Transition::Transition(int, char, char)
Transition.h:4:3: note: candidate expects 3 arguments, 0 provided
Transition.h:1:7: note: constexpr Transition::Transition(const Transition&)
Transition.h:1:7: note: candidate expects 1 argument, 0 provided
我是否需要以某种方式指定构造函数在实例化中使用的参数?我做错了什么?感谢您提前提供的帮助。
我总是使用map.insert(std::make_pair(keyForMap, transition));
。
std::map::operator[]
的工作原理如下:
-如果指定的键已经存在,它将返回对相应值的引用。
-如果指定的键不存在,它会插入它,为它指定一个默认值,并返回对最近创建的默认值实例的引用。
例如,map<string, int> m;
std::cout << m["new_key"]; // This prints the default value for type int, namely '0'.
如果您的值类型不支持默认值(例如,如果它是一个没有默认构造函数的自定义类),则不能使用std::map::operator[]
。但是,您可以使用std::map::insert
来执行相同的功能。后者将直接在地图中插入一对,而不经过创建默认值的中间步骤。
您的构造函数去掉了默认构造函数(这是必需的)。
相关文章:
- 递归无序映射
- 正在将无序映射设置为无序映射的值
- 智能指针作为无序映射键,并通过引用进行比较
- 打印无序映射的第二个元素,即集合
- 由并发无序映射查找线程调用的函数是否安全?
- 将大数字(10-12 位数字)存储在无序映射中<字符串,整数>
- 使用无序映射在STL中存储键值对
- 为C++中的无序映射获取给定输入键的错误值
- 将大型对象存储在无序映射中是否效率低下
- 在无序映射的结构化绑定中推导类型
- 使用无序映射进行错误索引
- 将一个向量对放在一个无序映射与一个映射中
- 如何在c++中修改无序映射中的每个值
- C++:使用一对(cpp_int,int)整数作为无序映射中的键(其中cpp_int是boost多精度整数)
- 将 [] 索引到无序映射时出现段错误
- 以std::字符串作为关键字,按字典顺序对一个无序映射进行排序
- 无序映射存储桶的节点大小
- 在无序映射<字符串上使用 find(),向量<string>>与 C++ 中的有序映射花费相同的时间
- 以三个无符号字符为关键字的无序映射
- 无序映射比c++中的映射慢吗