在c++中向映射中插入复杂值
Inserting complex values to a map in C++
我有麻烦插入数据到这个地图。老实说,我不知道如何做到这一点,但我给出的最后一行代码是我需要修复的部分。
map<string, vector<vector<Obj*>* >* > the_map;
vector<vector<Obj*> *>* vectors = new vector<vector<Obj*> *>;
vector<Obj*> Obj_vector;
vectors->push_back(&Obj_vector);
the_map.insert(make_pair(string("field1", &vectors)); //error on this line only
试试这个:
the_map.insert(make_pair(string("field1"), vectors));
//you forgot this ^ ^
// |
// & is not needed here
顺便说一下,我怀疑你的代码中使用了这么多指针,尤其是这两行:vector<Obj*> Obj_vector; //this is local variable
vectors->push_back(&Obj_vector); //inserting address of the local variable
将地址插入到vector中?
注意局部变量在超出作用域后将不存在,这反过来意味着您刚刚插入到vector中的地址指向被销毁的对象,并且使用它将调用未定义的行为。
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 预处理器:插入结构名称中的前一个行号
- 函数复杂度分析
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 在未初始化映射的情况下,将值插入到映射的映射中
- while循环中while循环的时间复杂度是多少
- 如何在c++中只将键插入到bimap的一侧
- 如何将结构插入到集合中并打印集合的成员
- C++json插入数组
- Visual Studio 2019:插入多个C++风格的单行注释
- nlohmann-json将一个数组插入到另一个数组中
- C++ - 最坏情况和平均情况插入时间复杂度在 std::unordered_map <int,int>?
- 将数组的元素插入映射的时间复杂度是多少?
- 将数据插入一些复杂的数据结构-C
- 将字符串插入到c++stl集的时间复杂度
- 在c++中向映射中插入复杂值
- 单链表插入和删除的时间复杂度
- 时间复杂度和插入std::list
- deque在顶部插入时是否提供0(1)复杂度?