wxArray usage! Topic is solved

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
tomay3000
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Mon Apr 24, 2017 4:23 am

wxArray usage!

Post by tomay3000 »

Hello,
Could anyone tell me how to declare and use a wxArray of a struct:
I thought wxArray is a template, just use "wxArray<My struct type>", but it is not.

and BTW: What is the diference between a wxList and a wxArray.

Thank you for your undestanding.
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxArray usage!

Post by doublemax »

tomay3000 wrote:Could anyone tell me how to declare and use a wxArray of a struct:
I thought wxArray is a template, just use "wxArray<My struct type>", but it is not.
You would need the macros described in the documentation to declare a new array type first:
http://docs.wxwidgets.org/trunk/classwx ... _01_4.html

However, it's discouraged to use these classes nowadays, use std containers instead, e.g. std::vector.
tomay3000 wrote:and BTW: What is the difference between a wxList and a wxArray.
They can do almost the same things, but because of differences internally, they perform differently for different tasks.

In an array all elements lie after each other in memory, so random access (array[17]) is fast. But if you want to add or remove an item in the middle, lots of data needs to get moved around and therefore it's slow.

A list is usually implemented as a "linked" list where each element has pointers to the previous and next element. This means that adding or removing elements in the middle of the list is fast, because you only need to change some pointers. But random access is slow, because to find the element with a specific index, you need to iterate over the elements until you reach the correct one.
Use the source, Luke!
tomay3000
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Mon Apr 24, 2017 4:23 am

Re: wxArray usage!

Post by tomay3000 »

Shame on me,
even thought I have read the documentation of both wxArray and wxList, and because I have never used them in wxWidgets before, I forget I studied Lists implementation in the university a long time ago #-o

it is very unfortunate that these container types usage is discouraged compared to the standard C++ STL ones :(

Thank you for the help.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxArray usage!

Post by ONEEYEMAN »

Hi,
Why do you say it is unfortunate?
STL is part of the standard now and wx containers are not.
In a recent wx version they are just a wrappers around the STL ones. So it will be much easier to just use STL directly.

Granted some API is unavoidable, but most of the time you should work with STL containers.

Thank you.
tomay3000
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Mon Apr 24, 2017 4:23 am

Re: wxArray usage!

Post by tomay3000 »

Because every new wxWidgets learner has to know its containers, and they are going to be replaced by the C++ standard ones in the future and wxUSE_STL and wxUSE_STD_CONTAINERS are not set by default.

and what will happen to wxArrayString e.g in the future!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxArray usage!

Post by ONEEYEMAN »

Hi,
No, they don't.
wxWidgets is a C++ library and if you don't know STL, you need to start with the C++.
And I would guess that it will be replaced with std::vector<wxString>.

Also, those container classes were created for very old C++ compilers that didn't support containers/STL. They are currently an ancient history, but because there are code out there that depends on them to be exist, wx developers can't deprecate them.

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxArray usage!

Post by doublemax »

FWIW, you can still use the wx containers and i don't think they'll be gone anytime soon.
Use the source, Luke!
tomay3000
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Mon Apr 24, 2017 4:23 am

Re: wxArray usage!

Post by tomay3000 »

Thank you guys for the help.
After a long reading I have decided to use wxVector template over wxArray macros. I have seen "Vadim Zeitlin" getting rid of wxArray implementation in the Github repo and replacing it with its wxVector equivalents.
take a look here: https://github.com/wxWidgets/wxWidgets/ ... 28ad6186ed
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxArray usage!

Post by doublemax »

Good catch. I totally had forgotten about wxVector.
Use the source, Luke!
raananb
Super wx Problem Solver
Super wx Problem Solver
Posts: 488
Joined: Fri Oct 27, 2006 4:35 pm
Location: Paris, France
Contact:

Re: wxArray usage!

Post by raananb »

@tomay3000
The decision to use wxVector is all yours. However, when quoting your source, you should quote it fully so there is no misunderstanding. The change on github submitted by vadz applies to a specific array, and does not imply that it is the end of array macros.
Use wxVector<> for wxBookCtrlBase::m_pages array

Get rid of another macro-based array in favour of wxVector<>.

No real changes.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxArray usage!

Post by PB »

To conclude, in your code:
1. Stay away from the old macro-based containers, they are relics of pre-STL times and not to be used.
2. Use standard containers such as std::vector over wxVector and wxArrayString, so you can avoid issues like this.
csniper
Experienced Solver
Experienced Solver
Posts: 53
Joined: Mon Mar 13, 2017 8:27 am

Re: wxArray usage!

Post by csniper »

Is there a official milestone for replacing the old macro-based containers? There are lots of API still using them as input/output.
PB wrote:To conclude, in your code:
1. Stay away from the old macro-based containers, they are relics of pre-STL times and not to be used.
2. Use standard containers such as std::vector over wxVector and wxArrayString, so you can avoid issues like this.
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxArray usage!

Post by doublemax »

Is there a official milestone for replacing the old macro-based containers? There are lots of API still using them as input/output.
No, but i don't think they will be dropped anytime soon. If ever.
Use the source, Luke!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxArray usage!

Post by PB »

Just to be sure: As I wrote in my comment the two points concerned your own code, obviously those old containers must be used when calling wxWidgets API.
Post Reply