what are std::vector holding pointers iterators?
Hello Guros,
I am new with C++ (and therefore with this Forum), so Hi ALL!!
I am writing a program that holds pointers (lets assume for int) in a std::vector.
I need to delete a specific given pointer (lets call it _toDelete) sitting in the vector, i use the for_each loop and a functor (called DeleteF) that holds the pointer _toDelete.
My question is:
The DeleteF functor will receive an iterator to the vector elements.
Will these elements be pure iterators, and i must reference the inside pointer with *currIter
or the given "iterator" is the pointer it self?
I ask it because untill now i worked with vector holding integral types and its iterators were pointers to the holded data.....now i work with a user define type pointer and it is risky.....:)
to make things clearer, i put my functor operator() implementation, UnReg is the Functor(:
void UnReg::operator()(vector< CallBackBase * >::iterator _currIter)
{
//!!!!!!pay attention to the below * operator for the iterator!!!!
if(*_currIter != m_toDelete)
{
return;
}
m_vector.erase(_currIter);
}
or maybe i am wrong and i derefernce my pointer and this is bad.......?
Thank you all!!!
hoshmy.
Re: what are std::vector holding pointers iterators?
I think you are overcomplicating the issue. Given a std::vector<int*> named v and an int* named toDelete, you can #include <algorithm> and write:
Code:
std::vector<int*>::iterator i = std::find(v.begin(), v.end(), toDelete);
if (i != v.end())
{
v.erase(i);
}
Re: what are std::vector holding pointers iterators?
o.k, Thanks alot!! that simplfies things!
But can i assume (i think u did it but i double check) that the iterators of vector<int*>v are the actuall pointers held in it?
Re: what are std::vector holding pointers iterators?
Quote:
Originally Posted by hoshmy
But can i assume (i think u did it but i double check) that the iterators of vector<int*>v are the actuall pointers held in it?
No, they are not. A vector<int*>::iterator is not an int*. You would dereference the vector<int*>::iterator to get an int*.
Re: what are std::vector holding pointers iterators?
Thanks again. I really appriciate it!
Re: what are std::vector holding pointers iterators?
Quote:
Originally Posted by
hoshmy
o.k, Thanks alot!! that simplfies things!
But can i assume (i think u did it but i double check) that the iterators of vector<int*>v are the actuall pointers held in it?
The type is "implementation" defined.
Usually, they are special iterators in debug builds, that can do things like behind the scene bounds cheking.
In release builds, they are usually straight up pointers, for performance reasons.
Re: what are std::vector holding pointers iterators?
Your functor needs to return a value. Also it does not receive an iterator type. It should take the value_type of the container. That simplifies things quite a bit. Consider this in accordance with what laserlight wrote except that you pass the pointer value to the functor during its construction. There seem to be some inconsistencies in the OP about what the functor is named but it matters not. Call it whatever you want so long as you implement it correctly.
Code:
// class attribute of functor
const CallBackBase*mPointer;
// constructor of functor
toDelete(const CallBackBase*pointer) : mPointer(pointer)
{
}
// must return bool and take value_type of container. In this case,
// your container contains CallBackBase pointers.
bool toDelete:operator()(const CallBackBase*pointer)
{
return (pointer == m_toDelete);
}
Re: what are std::vector holding pointers iterators?
Also keep in mind you are only erasing the pointers. The actual pointed-to objects are not deleted. This may or may not be your intension. Be careful.