Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Using std::set:...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 43643 of 47925
Post > Topic >>

Using std::set::erase with iterators

by mathieu <mathieu.malaterre@[EMAIL PROTECTED] > Mar 3, 2008 at 01:43 AM

Hi,

  I do not understand how I am supposed to use erase when looping over
elements in a set, here is what I am trying to do (*). Could someone
please indicate how was I supposed to do it properly ? All I can think
of is something like this (very ***bersome):

    if( *it % 2 )
      {
      std::set<int>::const_iterator duplicate = it;
      ++it;
      s.erase( duplicate );
      }
    else
      {
      ++it;
      }


Thanks
-Mathieu

(*)
#include <set>
#include <iostream>

int main()
{
  std::set<int> s;
  for(int i =0; i < 10; ++i)
    {
    s.insert( i );
    }

  for( std::set<int>::const_iterator it = s.begin(); it != s.end(); +
+it)
    {
    if( *it % 2 )
      s.erase( it );
    }

  for( std::set<int>::const_iterator it = s.begin(); it != s.end(); +
+it)
    {
    std::cout << *it << std::endl;
    }

  return 0;
}
 




 5 Posts in Topic:
Using std::set::erase with iterators
mathieu <mathieu.malat  2008-03-03 01:43:00 
Re: Using std::set::erase with iterators
Ondra Holub <ondra.hol  2008-03-03 02:20:58 
Re: Using std::set::erase with iterators
James Kanze <james.kan  2008-03-03 05:58:15 
Re: Using std::set::erase with iterators
James Kanze <james.kan  2008-03-03 05:59:50 
Re: Using std::set::erase with iterators
Abhishek Padmanabh <ab  2008-03-03 06:39:18 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 6 17:51:19 CDT 2008.