我有一个包含对象的二维向量。
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>&)
正如您可能从我的代码中看出的那样,我不是大师。任何建议都将受到高度赞赏!