By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

std::list

P: n/a
How can I erase one element over two in a list container?
I have done something like this:

1-
list<double> X;
list<double>::iterator iter;
for (int k=0; k<X.size() ; k++)
{
++iter;
if (!(k%2)) { X.pop_back(); }

}

2- I would like using something like this
for (iter=X.begin(); iter!=X.begin() ; iter++)
{

if (!(k%2)) { X.erase(iter); }

}
but doesn't work!

any suggustions?
thank you in advance
Dec 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Example:

int main()
{
std::list<int> the_list;
the_list.push_back(1);
the_list.push_back(-15);
the_list.push_back(3);
the_list.push_back(20);

std::list<int>::iterator pos;

for(pos = the_list.begin(); pos != the_list.end(); )
if(*pos < -10 || *pos > 10)
pos = the_list.erase(pos);
else
++pos;

std::copy(the_list.begin(), the_list.end(),
std::ostream_iterator<int>(std::cout, "\n"));

}

Dec 21 '05 #2

P: n/a
Example:

int main()
{
std::list<int> the_list;
the_list.push_back(1);
the_list.push_back(-15);
the_list.push_back(3);
the_list.push_back(20);

std::list<int>::iterator pos;

for(pos = the_list.begin(); pos != the_list.end(); )
if(*pos < -10 || *pos > 10)
pos = the_list.erase(pos);
else
++pos;

std::copy(the_list.begin(), the_list.end(),
std::ostream_iterator<int>(std::cout, "\n"));

}

Dec 21 '05 #3

P: n/a
Example:

int main()
{
std::list<int> the_list;
the_list.push_back(1);
the_list.push_back(-15);
the_list.push_back(3);
the_list.push_back(20);

std::list<int>::iterator pos;

for(pos = the_list.begin(); pos != the_list.end(); )
if(*pos < -10 || *pos > 10)
pos = the_list.erase(pos);
else
++pos;

std::copy(the_list.begin(), the_list.end(),
std::ostream_iterator<int>(std::cout, "\n"));

}

Dec 21 '05 #4

P: n/a
in 2 situation,

int k = 0;

for (iter=X.begin(); iter!=X.begin() ; iter++)
{
if (!(k++%2))
{
itr = X.erase(iter);
itr--;
}
}

Dec 21 '05 #5

P: n/a

Youssef Mesri wrote:
How can I erase one element over two in a list container?
I have done something like this:
[]
2- I would like using something like this
for (iter=X.begin(); iter!=X.begin() ; iter++)
{

if (!(k%2)) { X.erase(iter); }

}


Upon return from erase() iter is invalidated, you can't use it any
more. Fix:

for (iter = X.begin(); iter != X.end();)
{
if(some_condition)
x.erase(iter++);
else
++iter;
}

Dec 21 '05 #6

P: n/a
Youssef Mesri wrote:
How can I erase one element over two in a list container?
I have done something like this:

1-
list<double> X;
list<double>::iterator iter;
for (int k=0; k<X.size() ; k++)
{
++iter;
if (!(k%2)) { X.pop_back(); }

}

2- I would like using something like this
for (iter=X.begin(); iter!=X.begin() ; iter++)
{

if (!(k%2)) { X.erase(iter); }

}
but doesn't work!


Don't write a loop when one is not needed:

#include <list>

inline
bool IsEven(int n)
{
return not (n bitand 0x01);
}

int main (void)
{
std::list<int> theList;

// add some numbers to the list
theList.push_back(1);
theList.push_back(-8);
theList.push_back(-15);
theList.push_back(3);
theList.push_back(20);

// now remove every even number
theList.remove_if(IsEven);
...

Greg

Dec 21 '05 #7

P: n/a
Youssef Mesri wrote:
How can I erase one element over two in a list container?
I have done something like this:

1-
list<double> X;
list<double>::iterator iter;
for (int k=0; k<X.size() ; k++)
{
++iter;
if (!(k%2)) { X.pop_back(); }

}

2- I would like using something like this
for (iter=X.begin(); iter!=X.begin() ; iter++)
{

if (!(k%2)) { X.erase(iter); }

}
but doesn't work!


Don't write a loop when one is not needed:

#include <list>

inline
bool IsEven(int n)
{
return not (n bitand 0x01);
}

int main()
{
std::list<int> theList;

// add some numbers to the list
theList.push_back(1);
theList.push_back(-8);
theList.push_back(-15);
theList.push_back(3);
theList.push_back(20);

// now remove every even number
theList.remove_if(IsEven);
...

Greg

Dec 21 '05 #8

P: n/a
In your example, IsEven will receive the element value, not the element
pos.

try this:

struct IsEven {
IsEven(): pos(0) {}
bool operator()(int) {
return not (pos++ & 1);
}
private:
int pos;
};

....

theList.remove_if(IsEven(0));

-----

but it violates the predicate pure-function rule :(

Dec 21 '05 #9

P: n/a

Diego Martins wrote:
In your example, IsEven will receive the element value, not the element
pos.

try this:

struct IsEven {
IsEven(): pos(0) {}
bool operator()(int) {
return not (pos++ & 1);
}
private:
int pos;
};

...

theList.remove_if(IsEven(0));

-----

but it violates the predicate pure-function rule :(


IsEven is a misnomer. You'd really want a toggler.

class Toggler
{
bool flag;
public:
explicit Toggler( bool init=false ) : flag( init )
{
}

template < typename T >
bool operator() ( const T& )
{
return ( flag ^= true );
}
};

myList.remove_if( Toggler() );

Dec 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.