M
mathieu
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 cumbersome):
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;
}
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 cumbersome):
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;
}