Re: CArray, CStringArray, CMap, ...
"KW" <KW@there.com> ha scritto nel messaggio
I have a list of CStrings I would like to add to an array or list.
I would like to be able to:
Return the count of items.
Find a specific item.
Remove all of the items.
Allow the list/array to grow dynamically (could be 100 up to 10000
Of course, I can iterate through the items for each of these functions,
but I would like do this in the fastest way possible. It would also be a
plus if these operations were thread safe.
Are there any suggestions on the best route to take?
I would use std::vector template container class from STL.
std::vector works just fine with CString as contained item.
This code sample shows the points you requested:
// A list of strings stored in an array
typedef std::vector< CString > StringList;
// Create an empty collection
// Grows the collection dynamically
someStrings.push_back( _T("I'm the first string.") );
someStrings.push_back( _T("I'm the second string.") );
someStrings.push_back( _T("Hello") );
// 3 items in the container.
// Use size() method to get count of items.
ASSERT(someStrings.size() == 3);
// Search the string "Hello"
it = std::find( someStrings.begin(), someStrings.end(), _T("Hello") );
if ( it == someStrings.end() )
// Not found
// Remove all of the items
ASSERT(someStrings.size() == 0);