Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STL containers 2

Status
Not open for further replies.

maluk

Programmer
Oct 12, 2002
79
SG
I need help!
Why won't this work?

Code:
deque<ContactInfo>::iterator contact_info_iter;

for(contact_info_iter = contact_list.begin(); contact_info_iter != contact_list.end(); contact_info_iter++)
{
    contact_list.erase(contact_info_iter);
}

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
i have a solution but i don't think it is elegant.

Code:
deque<ContactInfo>::iterator contact_info_iter;
unsigned int current_contact_count = contact_list.size();

do {
  for(contact_info_iter = contact_list.begin(); contact_info_iter != contact_list.end(); contact_info_iter++)
  {
    if(contact_info_iter->isExpired())
    {
        // Current contact iterator has expired.
        contact_list.erase(contact_info_iter);
        break;
    }
    current_contact_count--;
  }
} while(current_contact_count);

anyone got any more ideas? solutions?

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
Well, how about:
Code:
contact_list.erase(contact_list.begin(),contact_list.end());
See also lots of articles with STL deque samples (with el by el erase() too) in MSDN.
 
> Why won't this work?

What's the expected behavior and what's the result you get?

i.e. compile-time error, run-time error, segmentation fault...?


Just so you know, when using iterators in for loops, you should normally use the ++i form of increment instead of the i++ form.

i++ causes the iterator to be copied before it is incremented so that a temporary copy with the previous value can be returned.

++i just increments the iterator and returns the iterator with the new value.

Unless you actually need the previous value, you shouldn't be generating it.

This only applies to user-defined iterators, not built-in ones (i.e. pointers). Still, if you use C++-style iterators at all it's a good habit to get into the practice of using the ++i form all the time unless you really need the behavior of i++.
 
Code:
contact_list.clear()
The reason the original code is wrong is because an erase can invalidate iterators in a deque. If you want to remove all elements in the deque, use the clear method above. If you want to remove some elements in a deque, you can use the remove_if algorithm, or a simple loop like this (notice how erase returns the next valid iterator):
Code:
std::deque<ContactInfo>::iterator contact_info_iter = contact_list.begin();
while(contact_info_iter != contact_list.end())
{
    if (!contact_info_iter->condition())
        contact_info_iter = contact_list.erase(contact_info_iter);
    else
        ++contact_info_iter;
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top