deleting node from wxList

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
papillon68
Earned some good credits
Earned some good credits
Posts: 118
Joined: Tue Nov 06, 2007 11:19 pm

deleting node from wxList

Post by papillon68 »

Hi all, I want to delete a wxList node while iterating. My first attempt, which crashes, was like this:

Code: Select all

	splineInventory::compatibility_iterator node = treeSplineInventory.GetFirst();
	while (node)
	{
		CRSpline *currentSpline  = node->GetData();			
		if (someCondition==true)
			treeSplineInventory.DeleteNode(nodeToDelete);
		
		node = node->GetNext();		
	}
Then I managed to work it out by instantiating the node to delete and move the iterator to next element. It works, but I'm not sure if it's an orthodox approach. I'd like to hear if there is better code to do it:

Code: Select all

	splineInventory::compatibility_iterator node = treeSplineInventory.GetFirst();
	while (node)
	{
		CRSpline *currentSpline  = node->GetData();			
		if (someCondition==true) {
			splineInventory::compatibility_iterator nodeToDelete = node;
			node = node->GetNext();
			treeSplineInventory.DeleteNode(nodeToDelete);
		}
		else node = node->GetNext();		
	}
Thanks for any clarification.
Windows 10, MS VC++ 2019 (vc142), WxWidgets 3.14
Designed with WxWidgets: https://www.facebook.com/clorofillaApp
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: deleting node from wxList

Post by PB »

wxWidgets documentation claims that wxList is std::list compatible. Therefore I suppose the orthodox way is to use the erase-remove idiom.

However, the old way you use should work but I guess std compatible containers are usually iterated differently, see e.g. wxList docs.

Edit
Here is an example of creating a list of integers and then removing odd numbers from it, without using the erase-remove idiom or Modern C++. It uses std::list but should be the same for wxList

Code: Select all

typedef std::list<int> IntList;
IntList myList;

for ( int i = 0; i < 10; ++i )
    myList.push_back(i);

IntList::iterator it = myList.begin();

while ( it != myList.end() )
{
    if ( (*it) % 2 ) 
        it = myList.erase(it);
    else
        ++it;                    
}
I cannot say it is the best way though.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: deleting node from wxList

Post by ONEEYEMAN »

Hi,
Also, just curious - why do you use wxList instead of std containers?

Thank you.
User avatar
papillon68
Earned some good credits
Earned some good credits
Posts: 118
Joined: Tue Nov 06, 2007 11:19 pm

Re: deleting node from wxList

Post by papillon68 »

I'm coming from the Qt world (where I stationed briefly): at that time I was advised to use dedicated containers rather than standard ones, so for example QList would have been better (?) than using std::vec or other standard containers. Is this not the case with WxWidgets? I mean, is better if I go and use standard containers rather than their Wx counterpart/implementation?
Sorry for all these questions...
Windows 10, MS VC++ 2019 (vc142), WxWidgets 3.14
Designed with WxWidgets: https://www.facebook.com/clorofillaApp
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: deleting node from wxList

Post by PB »

papillon68 wrote:I mean, is better if I go and use standard containers rather than their Wx counterpart/implementation?
wxWidgets containers originate from the era where STL was not available on all supported platforms. AFAIK, you should use standard containers in your code, unless wxWidgets API forces you to use one of their own.
Post Reply