[英]C++ nested map with unique_ptr
I'm currently learning c++ and focusing on the STL. 我目前正在学习c ++并专注于STL。 I didn't find the answer to this problem, so here is the issue: how to set elements in the data structure
map<int, map<string, vector<unique_ptr>>>
? 我没有找到这个问题的答案,所以问题是:如何在数据结构中设置元素
map<int, map<string, vector<unique_ptr>>>
? The following code with some comments illustrates this problem: 以下代码和一些注释说明了这个问题:
#include <map>
#include <string>
#include <memory>
#include <vector>
using namespace std;
// Used in the example
struct Resource {};
int main(int argc, char** argv) {
// I was able to get the following map running fine
// int -> { string -> unique_ptr }
map<int, map<string, unique_ptr<Resource>>> data;
map<string, unique_ptr<Resource>> toBeInserted;
toBeInserted["key"] = unique_ptr<Resource>(new Resource);
// data[1] = toBeInserted; // error
data[1] = std::move(toBeInserted); // ok
// But the issue happens when it's a vector of unique_ptrs
// int -> { string -> { [unique_ptr] } }
map<int, map<string, vector<unique_ptr<Resource>>>> otherData;
vector<unique_ptr<Resource>> list;
list.push_back(unique_ptr<Resource>(new Resource));
list.push_back(unique_ptr<Resource>(new Resource));
map<string, vector<unique_ptr<Resource>>> _toBeInserted;
_toBeInserted["key"] = std::move(list); // ok
// But I cant insert _toBeInserted back to the original map.
// The compilation errors are all related to the unique_ptr
otherData[1] = std::move(_toBeInserted); // Can't do this
}
-- edit: link to the compilation erros: ideone.com/hs3G8m - 编辑:链接到汇编错误: ideone.com/hs3G8m
My question is how to initialize and add elements to the structure map<int, map<string, vector<unique_ptr<T>>>>
. 我的问题是如何初始化和添加元素到结构
map<int, map<string, vector<unique_ptr<T>>>>
。 I'm using GCC 4.9 with c++11 flag. 我正在使用带有c ++ 11标志的GCC 4.9。 Thanks in advance!
提前致谢!
I do not see any problems with the code. 我没有看到代码有任何问题。 I have compiled it as
我把它编译成了
g++ -Wall -std=c++11 test.cppwithout any warnings using
gcc (GCC) 4.8.1gcc(GCC)4.8.1
It looks like a compiler/STL bug. 它看起来像编译器/ STL错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.