473,698 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::list safely pop out iterator

Hi,

I've got a class that uses a std::list to store tags that need to be
written out to a device. I've had no problem with the lists in terms
of iterating and reading values. My problem is when I need to pop that
value off the list when I'm done with it.

In this case, I think the iterator is getting "lost" because I pop off
the value it is pointing to at the end of this function. But how do I
pop values off one at a time without the iterator getting lost? The
erase function will just have the same problem.

Instead of hacking at it, I was wondering if anyone could point me
towards the standard solution when dealing with std::list.

Thanks in advance,
T

unsigned int duty_count;
std::list<CFusi onTag *> *mylist;
std::list<CFusi onTag *>::iterator it;
FU_Tag fu_tag;
FusionPlugin *myplg = m_pDevice->Driver();
CFusionTag *mytag;

mylist = m_pDevice->GetWriteTagLis t();
for( it=mylist->begin(), duty_count = 0;
it != mylist->end() && duty_count < m_pDevice->DutyCycle();
++it, ++duty_count )
{

mytag = *it;

if ( mytag->IsRegistered () )
{

if ( mytag->ClientAccess () == mytag->AccessWriteOnl y
|| mytag->ClientAccess () == mytag->AccessReadWrit e )
{
memcpy( fu_tag.Address, mytag->Address(), strlen( mytag-
>Address() ) );
fu_tag.DeviceID = m_pDevice->RKey();
fu_tag.TagID = mytag->RKey();
fu_tag.TagType = mytag->Type();
mytag->CreateFUTagVal ue( &fu_tag.Valu e );
mytag->GetValue( &fu_tag.Valu e );

int nret = myplg->m_PluginModule s->SetTagValue( &fu_tag );
if ( nret <= 0 )
{
mytag->Quality( 0 );
}

/* DELETE THE VALUE POINTER WE CREATED */
delete fu_tag.Value;

/* POP THIS OFF THE LIST */
mylist->pop_front();
}

}

}
Jan 8 '08 #1
4 3893
TBass wrote:
Hi,

I've got a class that uses a std::list to store tags that need to be
written out to a device. I've had no problem with the lists in terms
of iterating and reading values. My problem is when I need to pop that
value off the list when I'm done with it.

In this case, I think the iterator is getting "lost" because I pop off
the value it is pointing to at the end of this function. But how do I
pop values off one at a time without the iterator getting lost? The
erase function will just have the same problem.

Instead of hacking at it, I was wondering if anyone could point me
towards the standard solution when dealing with std::list.

Thanks in advance,
T

unsigned int duty_count;
std::list<CFusi onTag **mylist;
std::list<CFusi onTag *>::iterator it;
FU_Tag fu_tag;
FusionPlugin *myplg = m_pDevice->Driver();
CFusionTag *mytag;

mylist = m_pDevice->GetWriteTagLis t();
for( it=mylist->begin(), duty_count = 0;
it != mylist->end() && duty_count < m_pDevice->DutyCycle();
++it, ++duty_count )
{

mytag = *it;

if ( mytag->IsRegistered () )
{

if ( mytag->ClientAccess () == mytag->AccessWriteOnl y
>>mytag->ClientAccess () == mytag->AccessReadWrit e )
{
memcpy( fu_tag.Address, mytag->Address(), strlen( mytag-
>Address() ) );
fu_tag.DeviceID = m_pDevice->RKey();
fu_tag.TagID = mytag->RKey();
fu_tag.TagType = mytag->Type();
mytag->CreateFUTagVal ue( &fu_tag.Valu e );
mytag->GetValue( &fu_tag.Valu e );

int nret = myplg->m_PluginModule s->SetTagValue( &fu_tag );
if ( nret <= 0 )
{
mytag->Quality( 0 );
}

/* DELETE THE VALUE POINTER WE CREATED */
delete fu_tag.Value;

/* POP THIS OFF THE LIST */
mylist->pop_front();
}

}

}
Okay, you are using pop_front() to delete an element. How do you know that
it is pointing to the first element though? You are incrementing it in the
for loop, it may not be pointing to the first element. Wouldn't it be
better to do mylist->erase( it )? And erase returns an element to the next
item in the list. The normal algorithm for this is:

for ( std::list<CFusi onTag *>::iterator it = MyList.being(); it !=
MyList.end(); }
{
if ( somecondition )
it = MyList.erase( it );
else
++it;
}

Notice that it is not being incremented in the for loop. It is only
incremented if the element is not erased.

Now, if you really are wanting to erase the front elements only (which seems
like a bug in your code, but I may be wrong) then simply have it point to
the first element again after erasing it.
it = MyList->begin();

--
Jim Langston
ta*******@rocke tmail.com
Jan 8 '08 #2
In article
<cc************ *************** *******@x69g200 0hsx.googlegrou ps.com>,
TBass <tb*@automatedd esign.comwrote:
Hi,

I've got a class that uses a std::list to store tags that need to be
written out to a device. I've had no problem with the lists in terms
of iterating and reading values. My problem is when I need to pop that
value off the list when I'm done with it.

In this case, I think the iterator is getting "lost" because I pop off
the value it is pointing to at the end of this function. But how do I
pop values off one at a time without the iterator getting lost? The
erase function will just have the same problem.

Instead of hacking at it, I was wondering if anyone could point me
towards the standard solution when dealing with std::list.
I edited your code for brevity. As Jom Langston already noted, poping
the first object off of the list is not the correct thing to do.
for( it=mylist->begin(); it != mylist->end(); ++it )
{

mytag = *it;

if ( mytag->IsRegistered () )
{

if ( mytag->ClientAccess () == mytag->AccessWriteOnl y
|| mytag->ClientAccess () == mytag->AccessReadWrit e )
{

mylist->pop_front();
}
}

}
The idiomatic way to do this would be something like:

it = mylist.begin();
while ( it != mylist.end() )
{
if ( /* some condition */ )
it = mylist.erase( it );
else
++it;
}
Jan 8 '08 #3
* * * it = mylist.erase( it );

Ah. I did not know erase returned an iterator. Thank you both very
much.

Thanks!
Jan 9 '08 #4
TBass <tb*@automatedd esign.comwrote:
* * * it = mylist.erase( it );

Ah. I did not know erase returned an iterator. Thank you both very
much.
In the specific case of list, you don't need it to.

mylist.erase( it++ );

works too, but using the return is more generic.
Jan 9 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2861
by: ma740988 | last post by:
Consider: # include <iostream> using std::cout; using std::cin; using std::endl; # include <list> using std::list;
15
19729
by: sandwich_eater | last post by:
I want to know how to set an std::list iterator variable to make it null or nil. If this is not possible what is the value of an uninitialised std::list iterator and is it ok to assign this value to a std::list iterator variable (or is it better to use a seperate bool variable as a "null flag" ?) e.g. struct mystru { std::string nm;
6
6655
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const T*>::iterator?
16
3585
by: Frank Neuhaus | last post by:
Hi I have some std list, I'd like to traverse. During the traversal, I want to conditionally delete some objects. My code for that is like this right now: for (std::list<myStruct>::iterator it=myList.begin();it!=myList.end();) { if (it->someCondition) {
4
6538
by: Tim Slattery | last post by:
It would be convenient for my app to store the stuff I'm generating in a std::list. I'd like to remember the location of a particular place in the list - sort of like sticking my finger into it - and insert an entry in that place some time later. There's an "insert" member function, but it takes an iterator to designate the place to insert. And I can't figure out how to get an iterator that points to the right place. For instance: ...
7
2345
by: TBass | last post by:
So I have a class: class Client { unsigned int ClientID; .... }; class MyListenSocket
12
2698
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();
17
4170
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); }
11
4160
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter' still points to list1::end(). 2) 'iter' now points to list2::end().
0
8601
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,...
0
9156
Oralloy
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...
1
8892
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,...
0
7716
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
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...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4365
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...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3043
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.