Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;具有唯一\u ptr的嵌套映射 我现在正在学习C++,关注STL。我没有找到这个问题的答案,所以问题来了:如何在数据结构map中设置元素?以下代码和一些注释说明了此问题: #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 } #包括 #包括 #包括 #包括 使用名称空间std; //在示例中使用 结构资源{}; int main(int argc,字符**argv){ //我能够使下面的地图运行良好 //int->{string->unique\u ptr} 地图数据; 地图被插入; toBeInserted[“key”]=唯一的\u ptr(新资源); //数据[1]=待插入;//错误 数据[1]=std::move(toBeInserted);//确定 //但当它是一个独特的PTR向量时,问题就发生了 //int->{string->{[unique_ptr]} 地图数据; 向量表; list.push_back(唯一_ptr(新资源)); list.push_back(唯一_ptr(新资源)); 要插入的地图; _toBeInserted[“key”]=std::move(list);//确定 //但是我无法插入到原始地图。 //编译错误都与唯一的\u ptr有关 otherData[1]=std::move(_toBeInserted);//无法执行此操作 }_C++_Stl_Smart Pointers - Fatal编程技术网 {string->unique\u ptr} 地图数据; 地图被插入; toBeInserted[“key”]=唯一的\u ptr(新资源); //数据[1]=待插入;//错误 数据[1]=std::move(toBeInserted);//确定 //但当它是一个独特的PTR向量时,问题就发生了 //int->{string->{[unique_ptr]} 地图数据; 向量表; list.push_back(唯一_ptr(新资源)); list.push_back(唯一_ptr(新资源)); 要插入的地图; _toBeInserted[“key”]=std::move(list);//确定 //但是我无法插入到原始地图。 //编译错误都与唯一的\u ptr有关 otherData[1]=std::move(_toBeInserted);//无法执行此操作 },c++,stl,smart-pointers,C++,Stl,Smart Pointers" /> {string->unique\u ptr} 地图数据; 地图被插入; toBeInserted[“key”]=唯一的\u ptr(新资源); //数据[1]=待插入;//错误 数据[1]=std::move(toBeInserted);//确定 //但当它是一个独特的PTR向量时,问题就发生了 //int->{string->{[unique_ptr]} 地图数据; 向量表; list.push_back(唯一_ptr(新资源)); list.push_back(唯一_ptr(新资源)); 要插入的地图; _toBeInserted[“key”]=std::move(list);//确定 //但是我无法插入到原始地图。 //编译错误都与唯一的\u ptr有关 otherData[1]=std::move(_toBeInserted);//无法执行此操作 },c++,stl,smart-pointers,C++,Stl,Smart Pointers" />

C++;具有唯一\u ptr的嵌套映射 我现在正在学习C++,关注STL。我没有找到这个问题的答案,所以问题来了:如何在数据结构map中设置元素?以下代码和一些注释说明了此问题: #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 } #包括 #包括 #包括 #包括 使用名称空间std; //在示例中使用 结构资源{}; int main(int argc,字符**argv){ //我能够使下面的地图运行良好 //int->{string->unique\u ptr} 地图数据; 地图被插入; toBeInserted[“key”]=唯一的\u ptr(新资源); //数据[1]=待插入;//错误 数据[1]=std::move(toBeInserted);//确定 //但当它是一个独特的PTR向量时,问题就发生了 //int->{string->{[unique_ptr]} 地图数据; 向量表; list.push_back(唯一_ptr(新资源)); list.push_back(唯一_ptr(新资源)); 要插入的地图; _toBeInserted[“key”]=std::move(list);//确定 //但是我无法插入到原始地图。 //编译错误都与唯一的\u ptr有关 otherData[1]=std::move(_toBeInserted);//无法执行此操作 }

C++;具有唯一\u ptr的嵌套映射 我现在正在学习C++,关注STL。我没有找到这个问题的答案,所以问题来了:如何在数据结构map中设置元素?以下代码和一些注释说明了此问题: #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 } #包括 #包括 #包括 #包括 使用名称空间std; //在示例中使用 结构资源{}; int main(int argc,字符**argv){ //我能够使下面的地图运行良好 //int->{string->unique\u ptr} 地图数据; 地图被插入; toBeInserted[“key”]=唯一的\u ptr(新资源); //数据[1]=待插入;//错误 数据[1]=std::move(toBeInserted);//确定 //但当它是一个独特的PTR向量时,问题就发生了 //int->{string->{[unique_ptr]} 地图数据; 向量表; list.push_back(唯一_ptr(新资源)); list.push_back(唯一_ptr(新资源)); 要插入的地图; _toBeInserted[“key”]=std::move(list);//确定 //但是我无法插入到原始地图。 //编译错误都与唯一的\u ptr有关 otherData[1]=std::move(_toBeInserted);//无法执行此操作 },c++,stl,smart-pointers,C++,Stl,Smart Pointers,--编辑:链接到编译错误: 我的问题是如何初始化并将元素添加到结构map。我正在使用带有c++11标志的GCC4.9。提前谢谢 我看不出代码有任何问题。 我将其编译为g++-Wall-std=c++11 test.cpp g++ -Wall -std=c++11 test.cpp 使用gcc(gcc)4.8.1没有任何警告 gcc (GCC) 4.8.1 它看起来像一个编译器/STL错误。向量列表我建议不要将变量命名为与STL类相同的名称,即列表。。。。特别是如果你是那种使用名称空间std做的人

--编辑:链接到编译错误:


我的问题是如何初始化并将元素添加到结构
map
。我正在使用带有c++11标志的GCC4.9。提前谢谢

我看不出代码有任何问题。 我将其编译为g++-Wall-std=c++11 test.cpp g++ -Wall -std=c++11 test.cpp 使用gcc(gcc)4.8.1没有任何警告

gcc (GCC) 4.8.1
它看起来像一个编译器/STL错误。

向量列表我建议不要将变量命名为与STL类相同的名称,即
列表
。。。。特别是如果你是那种使用名称空间std做
的人此处:为什么要在此处使用
unique\ptr
?如果你把一个
唯一的\u ptr
放在
向量中,那么当你想要取消引用它时,你必须首先将它移出,因为你不允许复制一个。似乎
shared\u ptr
是您试图实现的目标的合适类型。使用
std::shared\u ptr
,使用相同的代码: