我就是这样做的,通过使用自定义排序谓词std::string
#include <algorithm>
#include <iostream>
#include <locale>
#include <string>
struct SortNoCase // functor used to sort strings ignoring the case
{
bool operator()(const std::string& lhs, const std::string& rhs) const
{
std::string lhs_lower, rhs_lower;
std::transform(std::begin(lhs), std::end(lhs),
std::back_inserter(lhs_lower), ::tolower);
std::transform(std::begin(rhs), std::end(rhs),
std::back_inserter(rhs_lower), ::tolower);
return lhs_lower < rhs_lower;
}
};
int main ()
{
std::vector<std::string> vs{"Some", "strings", "THAT", "are", "UnSorted"};
std::sort(std::begin(vs), std::end(vs), SortNoCase());
for(auto&& elem: vs)
std::cout << elem << " ";
std::cout << std::endl;
}
PS:还有更复杂的方法,比如使用自定义 char 特征,但是这样做并且很容易理解。如果你真的是古玩,可以看这里:
https://stackoverflow.com/a/5319855/3093378