2012-04-15 110 views
1

我有一个包含对象的2D矢量。迭代通过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 | 

因此,在上述表示我有3行向量与4山坳,所以它说删除那将是对象的位置已被删除的地方。

我猜测,一旦对象已经从内存中删除,向量中的空间就会......'零'?

我应该注意,如果对象获得与Say [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>&) 

你大概可以从我的代码告诉,我没有主人..任何建议将不胜感激!

回答

1

在你的第一个代码块中,你定义了一个std::vector<std::vector<List> >,即一个矢量的向量列表和在你循环第二个块std::vector<std::vector<List*> >,即指向List的向量矢量。因此,应用的迭代器是不能相互转换的不同类型。

我建议使用typedefs作为内部和外部向量,以确保您的类型一致。

请记住,通过迭代器擦除元素将使迭代器无效,但会返回指向下一个元素的新迭代器。