2012-04-15 81 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作爲內部和外部向量,以確保您的類型一致。

請記住,通過迭代器擦除元素將使迭代器無效,但會返回指向下一個元素的新迭代器。