循环访问 2D 矢量以从内存中删除对象

Iterating through 2D vector to delete objects from memory

本文关键字:内存 删除 对象 访问 2D 循环      更新时间:2023-10-16

我有一个包含对象的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,以确保您的类型一致。

请记住,由迭代器擦除元素会使迭代器

无效,但会返回指向下一个元素的新迭代器。