How can I erase one element over two in a list container?
I have done something like this:
1-
list<double> X;
list<double>::i terator 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 9 9099
Example:
int main()
{
std::list<int> the_list;
the_list.push_b ack(1);
the_list.push_b ack(-15);
the_list.push_b ack(3);
the_list.push_b ack(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_l ist.begin(), the_list.end(),
std::ostream_it erator<int>(std ::cout, "\n"));
}
Example:
int main()
{
std::list<int> the_list;
the_list.push_b ack(1);
the_list.push_b ack(-15);
the_list.push_b ack(3);
the_list.push_b ack(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_l ist.begin(), the_list.end(),
std::ostream_it erator<int>(std ::cout, "\n"));
}
Example:
int main()
{
std::list<int> the_list;
the_list.push_b ack(1);
the_list.push_b ack(-15);
the_list.push_b ack(3);
the_list.push_b ack(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_l ist.begin(), the_list.end(),
std::ostream_it erator<int>(std ::cout, "\n"));
}
in 2 situation,
int k = 0;
for (iter=X.begin() ; iter!=X.begin() ; iter++)
{
if (!(k++%2))
{
itr = X.erase(iter);
itr--;
}
}
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_conditi on)
x.erase(iter++) ;
else
++iter;
}
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>::i terator 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_ba ck(1);
theList.push_ba ck(-8);
theList.push_ba ck(-15);
theList.push_ba ck(3);
theList.push_ba ck(20);
// now remove every even number
theList.remove_ if(IsEven);
...
Greg
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>::i terator 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_ba ck(1);
theList.push_ba ck(-8);
theList.push_ba ck(-15);
theList.push_ba ck(3);
theList.push_ba ck(20);
// now remove every even number
theList.remove_ if(IsEven);
...
Greg
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 :(
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_i f( Toggler() ); This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mike Pemberton |
last post by:
I'm sure there's a good explanation for this effect, but I get rather
a strange output from this little test:
#include <iostream>
#include <list>
int main()
{
std::list<int> int_list;
|
by: JustSomeGuy |
last post by:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.
What methods of the std stl list class must Ioverride in order for this
to work?
|
by: Eric Lilja |
last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope
it's readable at least):
#include <algorithm>
#include <iostream>
#include <vector>
class Row
{
public:
|
by: Josh Mcfarlane |
last post by:
Just out of curiosity:
When would using std::list be more efficient / effective than using
other containers such as vector, deque, etc?
As far as I'm aware, list doesn't appear to be specialized for
anything.
Thanks,
Josh McFarlane
|
by: alex221 |
last post by:
In need to implement a tree structure in which every node has arbitrary
number of children the following code has come into mind:
using std::list;
template < class Contents class Tree_node{
Contents cn;
list < BTree_node < Contents children;
........
| |
by: Spoon |
last post by:
Hello,
Could someone explain why the following code is illegal?
(I'm trying to use a list of (C-style) arrays.)
#include <list>
typedef std::list < int foo_t;
int main()
{
int v = { 12, 34 };
|
by: Javier |
last post by:
Hi all,
I have this code:
class A
{
std::list<Bm_observadores;
void function()
{
|
by: Ray D. |
last post by:
Hey all,
I'm trying to pass a list into a function to edit it but when I
compile using g++ I continue to get the following error:
maintainNeighbors.cpp:104: error: invalid initialization of non-const
reference of type 'std::list<HostID, std::allocator<HostID&' from a
temporary of type 'std::list<HostID, std::allocator<HostID*'
helpers.cpp:99: error: in passing argument 1 of `void
|
by: isliguezze |
last post by:
template <class T>
class List {
public:
List();
List(const List&);
List(int, const T&);
void push_back(const T &);
void push_front(const T &);
void pop_back();
|
by: Isliguezze |
last post by:
Does anybody know how to make a wrapper for that iterator? Here's my
wrapper class for std::list:
template <class Tclass List {
private:
std::list<T*lst;
public:
List() { lst = new std::list<T>(); }
List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); }
List(int n, const T& value) { lst = new std::list<T>(n, value); }
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |