循环访问 2D 矢量以从内存中删除对象
Iterating through 2D vector to delete objects from memory
我有一个包含对象的2D向量。
std::vector<std::vector<List> > ListPos;
ListPos.clear();
std::vector<List> initPV;
ListPos.push_back(initPV);
List newList;
//... some code to determine where the object needs to go and vector resized to accommodate ...//
ListPos[ThisY].insert(ListPos[ThisY].begin()+ThisX, newList);
创建对象并根据需要调整矢量大小,我的问题是如何遍历矢量并delete
我不使用的任何对象(给定一些位置数据,例如 if(![3][7])
以释放内存。
我还可以对矢量执行任何操作,以释放对象删除后使用的空间的内存吗?
| List | List | List |
-------------------------------
| List | List | Delted | List |
-------------------------------
| Deleted | List |
因此,在上面的表示中,我有一个最多 4 列的 3 行向量,因此它说已删除的地方将是已删除对象位置的位置。
我猜一旦对象从内存中删除,矢量中的空间就会.."零"?
我应该注意,如果对象被删除,[2][0]
我需要留出可用对象来代替它,但如果这有意义的话,我不能让[2][1]
取代它的位置。 [2][1]
需要留在[2][1]
我尝试了以下(实际代码)
for (std::vector<std::vector<List*> >::iterator i = Area::AreaControl.ListPos.begin(); i != Area::AreaControl.ListPos.end();++i)
{
for (std::vector<List*>::iterator j = i->begin(); j != i->end();++i)
{
if(j != Area::AreaControl.ListPos[0][0]) {
// Delete
}
}
}
但是没有骰子:(
error: conversion from ‘std::vector<List>::iterator {aka __gnu_cxx::__normal_iterator<List*, std::vector<List> >}’ to non-scalar type ‘std::vector<List*>::iterator {aka __gnu_cxx::__normal_iterator<List**, std::vector<List*> >}’ requested
src/Void_OnLoop.cpp:62:73: error: no match for ‘operator!=’ in ‘j != i.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> [with _Iterator = std::vector<List>*, _Container = std::vector<std::vector<List> >, __gnu_cxx::__normal_iterator<_Iterator, _Container>::pointer = std::vector<List>*]()->std::vector<_Tp, _Alloc>::end [with _Tp = List, _Alloc = std::allocator<List>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<List*, std::vector<List> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = List*]()’
src/Void_OnLoop.cpp:62:73: note: candidates are:
/usr/include/c++/4.6/ext/new_allocator.h:128:5: note: template<class _Tp> bool __gnu_cxx::operator!=(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&)
/usr/include/c++/4.6/bits/stl_iterator.h:817:5: note: template<class _Iterator, class _Container> bool __gnu_cxx::operator!=(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
/usr/include/c++/4.6/bits/stl_iterator.h:811:5: note: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator!=(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
/usr/include/c++/4.6/bits/streambuf_iterator.h:200:5: note: template<class _CharT, class _Traits> bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
/usr/include/c++/4.6/bits/basic_string.h:2497:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/basic_string.h:2485:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2473:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/postypes.h:223:5: note: template<class _StateT> bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/include/c++/4.6/bits/stl_vector.h:1297:5: note: template<class _Tp, class _Alloc> bool std::operator!=(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/allocator.h:137:5: note: template<class _Tp> bool std::operator!=(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
正如你可能从我的代码中看出的那样,我不是大师。任何建议将不胜感激!
在你的第一个代码块中,你定义一个std::vector<std::vector<List> >
,即List向量的向量,在第二个块中你循环std::vector<std::vector<List*> >
,即指向List的指针向量的向量。因此,应用的迭代器是不同的类型,无法相互转换。
我建议对内部和外部向量使用 typedef,以确保您的类型一致。
请记住,由迭代器擦除元素会使迭代器无效,但会返回指向下一个元素的新迭代器。
相关文章:
- 迭代时从向量和内存中删除对象
- 为什么示例代码访问IUnknown中已删除的内存
- 我们可以删除链表中静态内存中的节点吗
- 在c++中删除内存失败
- 如何在cpp中删除(免费)多维结构内存?
- 如何删除列出的"QGraphicsPathItem"对象以控制进程内存使用情况?
- 一种从内存中删除 UTF 字节的方法?
- 内存泄漏如何查找和删除
- 擦除是否删除 stl 无序列图元素使用的堆内存
- 无法删除布尔动态内存分配
- 删除类成员的动态分配内存的最佳方法是什么
- 如何控制或优化或删除或释放 UNION 中未使用的内存
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- 为什么删除分配的阵列会导致内存错误?
- 删除指针后内存泄漏
- 提升进程间共享内存删除、权限和输出文件
- 升级分解共享内存删除对象而无需销毁
- C++内存:删除未使用的布尔数组可以将结果从正确更改为错误
- 函数内的动态内存删除
- 内存删除的运行时检测