c++ - Set a default constructor for an element in an unordered_map in case of [] operator
问题描述
I have this class:
class test_t {
public:
int value;
test_t() { }
test_t(int _value) : value(_value) { }
};
Now I have create a unordered_map with the a int value as key
std::unordered_map<int, test_t> map;
When I will use the operator [] if the key it not exist a new element will be add to the map calling the construct.
test_t & test = map[0];
Now it si possible to tell to the unordered_map to call the other constructor? i.e. is is possibile to do something like this?
std::unordered_map<int, test_t(5)> map;
in the means that every new element will be create whit the construction with value 5?
I know that i can create a construction like this:
test_t(int _value = 5) { }
however the class test is only a example of something more complex.
解决方案
推荐阅读
- git - 当我推送我的 repo 时,如何自动将项目中的 post-receive 文件与 git hook 目录中的 post-receive 文件同步?
- javascript - 角度嵌套表单错误 - 循环对象值
- angular - 如何动态创建角度组件并附加到顶部而不是底部?
- python - 我如何解决 InvalidArgumentError: cycle_length must be > 0 when load tfrecords file
- c# - ASP.NET Telerik 组合框未使用新值更新
- javascript - typescript Success Callback 方法中 Response.Redirect (ASP.NET) 的替代方法是什么?
- swift - SwiftUI - 如何访问列表中的数据
- c# - 将 C 结构转换为 C#?
- python - PyCharm 和 PyQT QFileDialog 之间的奇怪交互
- flutter - 无法确定任务“:google_maps_flutter:compileDebugAidl”的依赖关系