You have to provide a 'comp' parameter which behaves case insensitive like you want (note, though, that it has to be a model for the concept Strict Weak Ordering, so no behavior like '<=' but like '<' is required) |
I think that this is the easiest method of doing it, especially if you are new to c++ and the STL.
As
exception mentioned one interface of the sort() function is:
void sort(RAI first, RAI last, StrictWeakOrdering comp)
That means that except from the start and end position iterators, it can also accept a function name which will provide it a
Strict Weak Ordering.
So, you can make your own function for comparing the strings ( classes, structs, or whatever you want ).
I did it like that:
1 2 3 4 5 6 7 8 9 10
|
bool stringCompare( const string &left, const string &right ){
for( string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit )
if( tolower( *lit ) < tolower( *rit ) )
return true;
else if( tolower( *lit ) > tolower( *rit ) )
return false;
if( left.size() < right.size() )
return true;
return false;
}
|
and the call to the sort() function was:
sort( strs.begin(), strs.end(), stringCompare );
To explain a little my function:
The for-loop will run through the elements of the strings and will compare them by converting each letter to lower case.
If the strings have different sizes then the for-loop will check only the char positions they both have (so the iterators won't go offlimits).
If both strings are identical after the end of the loop then it will check the size. If the size() of the left string is bigger than the size of the right string it will return true so they can change positions in the sort.
Hope this helps.
[EDIT]
@
satm2008
Telling him to use a
simple linked list wont solve his problem, he will still has to deal with it. Also he will have to make his own sorting function and much more that vectors have already...