2012-04-15 3 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); 

객체를 생성하고 필요에 따라 크기를 조정할 벡터가, 내 질문에 메모리를 확보하는 등 if(![3][7]) 나는이 벡터 내가 사용하지 않는 delete 물건을 통해 루프 (일부 위치 데이터를 제공 할 수있는 방법이다된다.

또한 나는이 삭제 된 후 개체가 사용하던 공간 메모리를 확보하기 위해 벡터로 아무것도 할 수 있습니까?

| List | List | List | 
------------------------------- 
| List | List | Delted | List | 
------------------------------- 
| Deleted | List | 

그래서 위의 표현에서 나는 최대 4와 3 행 벡터를 cols, 그래서 그것이 말하는 곳 개체의 위치가 삭제 된 위치가 삭제됩니다.

일단 개체가 메모리에서 삭제되면 벡터의 공간이 단지 '제로'가 될 것이라고 추측합니다.

내가 말하는 곳에서 객체가 삭제되는 위치에주의해야합니다. [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> >을 정의합니다. 즉 벡터의 벡터는 List입니다. 두 번째 블록은 std::vector<std::vector<List*> >에 걸쳐 반복됩니다. 즉, List에 대한 포인터 벡터의 벡터입니다. 따라서 적용된 반복자는 서로 변환 할 수없는 다른 유형입니다.

유형을 일관되게 유지하려면 내부 및 외부 벡터에 typedef를 사용하는 것이 좋습니다.

반복기로 요소를 지우는 것은 반복기를 무효화하지만 다음 요소를 가리키는 새로운 반복기를 반환한다는 점을 기억하십시오.

관련 문제