Reputation: 2275
I have a 2D vector which contains objects.
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);
Objects are created and the vector resized as needed, my question is how can I loop through the vector and delete
any objects I'm not using(given some location data such asif(![3][7])
to free up memory.
Also can I do anything with the vector to free up memory for the space that the object was using after it being deleted?
| List | List | List |
-------------------------------
| List | List | Delted | List |
-------------------------------
| Deleted | List |
So in the above representation I have a 3 row vector with up to 4 cols, so where it says deleted that would be where the position of the objects have been deleted from.
I'm guessing that once the objects had been deleted from memory that the space in the vector would just.. 'zero'?
I should note that where objects get deleted from say [2][0]
I need to leave available for another object to go in its place, but cant allow [2][1]
to take its place, if that makes sense. [2][1]
needs to stay at [2][1]
I have tried the following (actual code)
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
}
}
}
But no dice :(
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>&)
As you can probably tell from my code, I'm no master.. Any suggestions will be highly appreciated!
Upvotes: 1
Views: 649
Reputation: 1713
In your first code block you define a std::vector<std::vector<List> >
, i.e., a vector of vectors of List and in the second block you loop over std::vector<std::vector<List*> >
, i.e., a vector of vector of pointer to List. Hence, the applied iterators are different types which cannot be converted into each other.
I'd recommend to use typedefs for the inner and outer vector to ensure your types are consistent.
And remember that erasing an element by an iterator invalidates the iterator, but returns a new iterator pointing to the next element.
Upvotes: 1