Re: sorting std::vector<string> ignoring case
- From: Darko Miletic <darko.miletic@xxxxxxxxxxx>
- Date: Tue, 08 May 2007 16:52:42 -0300
Ed wrote:
I need to sort a vector of strings ignoring case while preserving it. That is, if I have a vector containing "Xyz" and "abc" I
want to report "abc" "Xyz". I can think of several inelegant ways, such as creating a second vector with all upper (or lower)
and sort it while saving the sort order in an integer array, using the latter to index the while reporting the original vector...
that is, an indirect sort. But I was wondering if there was some why of accomplishing this more directly with the
STL algorithms.
Here is the sample that works:
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
struct LessNoCase {
bool operator()(const std::string &x, const std::string &y) const {
return (stricmp (x.c_str(), y.c_str()) < 0);
}
};
int main() {
std::vector<std::string> strVec;
strVec.push_back("Xyz");
strVec.push_back("abc");
std::sort( strVec.begin(),
strVec.end() ,
LessNoCase() );
std::copy( strVec.begin(),
strVec.end() ,
std::ostream_iterator<std::string>(std::cout,"\n"));
return 0;
}
.
- Follow-Ups:
- Re: sorting std::vector<string> ignoring case
- From: Cholo Lennon
- Re: sorting std::vector<string> ignoring case
- References:
- Prev by Date: Re: sorting std::vector<string> ignoring case
- Next by Date: Re: sorting std::vector<string> ignoring case
- Previous by thread: Re: sorting std::vector<string> ignoring case
- Next by thread: Re: sorting std::vector<string> ignoring case
- Index(es):
Relevant Pages
|
Loading