what are std::vector holding pointers iterators?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: what are std::vector holding pointers iterators?

  1. #1
    Join Date
    Jun 2010
    Posts
    4

    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:perator()(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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

    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);
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2010
    Posts
    4

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

    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*.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jun 2010
    Posts
    4

    Re: what are std::vector holding pointers iterators?

    Thanks again. I really appriciate it!

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,281

    Re: what are std::vector holding pointers iterators?

    Quote Originally Posted by hoshmy View Post
    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.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb 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);
    }
    Last edited by kempofighter; June 28th, 2010 at 12:02 PM.

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,281

    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.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center