OVERVIEW
The wxArrayString is a class which acts as a wrapper for the standard vector class in C++. This enables us to create a dynamic list of wxStrings and then do normal array operations with them.
SAMPLE CODE
Given below is sample code which uses wxArrayString in three different ways.
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif // WX_PRECOMP
#include <wx/string.h>
#include <wx/arrstr.h>
void printArr(wxArrayString* arr) {
for(size_t i=0; i < arr->GetCount(); i++) {
wxString val = arr->Item(i);
wxPuts(val);
}
}
int main(int argc, char **argv) {
wxInitialize();
wxPuts("Starting..");
wxArrayString *arr1 = new wxArrayString();
arr1->Add("one"); arr1->Add("two"); arr1->Add("three");
wxString retVal = "size of arr1=" + wxString::Format(wxT("%zu"), arr1->GetCount());
wxPuts(retVal);
printArr(arr1);
wxPuts("Sorting in ascending order");
arr1->Sort();
printArr(arr1);
arr1->Clear();
retVal = "size of arr1 after clearing=" + wxString::Format(wxT("%zu"), arr1->GetCount());
wxPuts(retVal);
wxArrayString arr2;
arr2.Alloc(10);
for(size_t i=0; i < 20; i++) {
wxString s = wxString::Format(wxT("%zu"), i);
arr2.Add(s);
}
retVal = "size of arr2 =" + wxString::Format(wxT("%zu"), arr2.GetCount());
printArr(&arr2);
int searchElem = arr2.Index(wxT("3"));
retVal = "found '3' in element " + wxString::Format(wxT("%d"), searchElem);
wxPuts(retVal);
arr2.RemoveAt(searchElem);
searchElem = arr2.Index(wxT("3"));
retVal = "location of '3' after removal " + wxString::Format(wxT("%d"), searchElem);
wxPuts(retVal);
arr2.Shrink();
wxString test = wxT("one,two,three,four,five");
wxPuts("String values:" + test);
wxArrayString arr3 = wxSplit(test, wxT(','));
printArr(&arr3);
wxPuts("Ending..");
wxUninitialize();
return 1;
}
The sample output is given below:
Leave a Reply