How do you use iterators with vectors of class pointers?

This is a discussion on How do you use iterators with vectors of class pointers? within the C++ Programming forums, part of the General Programming Boards category; Hello! I'm very new to STL and have run across a fairly rudimentary question. Let's say I've got a vector ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    How do you use iterators with vectors of class pointers?

    Hello! I'm very new to STL and have run across a fairly rudimentary question. Let's say I've got a vector as follows:

    Code:
    vector<Tangible*> tangibles;
    Let's say I've got the following function definition:

    Code:
    Tangible* theReturn = NULL;
    vector<Tangible*>::iterator iterator;
    
    for (iterator = tangibles.begin(); iterator != tangibles.end(); 
            iterator++)
    {
        if (iterator->get_tangibleType() == tangibleType)
        {
            theReturn = iterator;
            tangibles.erase(iterator);
            break;
        }
    }
    return theReturn;
    Apparently that doesn't work too well. Basically I just want to go through tangibles, find the first element whose "tangibleType" is equals to the value specified, set theReturn to that element (which is a pointer), pull the element out of the vector, and return theReturn. How should this block of code be written? Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    You should have dereferenced the iterator, i.e.,
    Code:
    if ((*iterator)->get_tangibleType() == tangibleType)
    {
        theReturn = *iterator;
        tangibles.erase(iterator);
        break;
    }
    Also, instead of explicitly using a loop, consider using std::find.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100
    Thanks, laserlight!

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    100
    In C::B, when I try to call the function from the dereferenced iterator directly, as in line 1, I get an error that is too obfuscated to make any use of. However when I set theReturn to the dereferenced iterator, as in line 3, and then try to call the function from theReturn, everything works out fine. Why?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,352
    What error do you get? "Too obfuscated" is subjective
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,604
    tangibles.erase(iterator);
    If no other object is cleaning up the memory in the container then this is a memory leak.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    100
    VirtualAce, how so?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    VirtualAce assumed that somewhere before this code, you are using new or new[] to set the value of the pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors in vectors - pointers and iterators
    By fisherking in forum C++ Programming
    Replies: 8
    Last Post: 07-27-2010, 09:34 AM
  2. Vectors and Iterators
    By Trev614 in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2008, 10:30 AM
  3. vectors and iterators?
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2007, 11:39 AM
  4. Vectors, iterators and loops
    By Heavens in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2007, 06:05 AM
  5. vectors with same iterators
    By strickey in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2005, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21