Re: CArray, CStringArray, CMap, ...
"KW" <KW@there.com> ha scritto nel messaggio
news:eSaP9Y5tJHA.5452@TK2MSFTNGP02.phx.gbl...
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
items).
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:
<code>
// A list of strings stored in an array
typedef std::vector< CString > StringList;
// Create an empty collection
StringList someStrings;
// 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"
StringList::iterator it;
it = std::find( someStrings.begin(), someStrings.end(), _T("Hello") );
if ( it == someStrings.end() )
{
// Not found
}
else
{
// Found
}
// Remove all of the items
someStrings.clear();
ASSERT(someStrings.size() == 0);
</code>
HTH,
Giovanni