Chr*_*_45 5 c++ arrays sorting string
如何在C++中对字符串数组进行排序,以便按以下顺序进行:
安卡先生
布朗先生
塞瑟先生
mR donK
先生
Ätt先生
先生
//following not the way to get that order regardeless upper or lowercase and å, ä, ö
//in forloop...
string handle;
point1 = array1[j].find_first_of(' ');
string forename1(array1[j].substr(0, (point1)));
string aftername1(array1[j].substr(point1 + 1));
point2 = array1[j+1].find_first_of(' ');
string forename2(array1[j+1].substr(0, (point2)));
string aftername2(array1[j+1].substr(point2 + 1));
if(aftername1 > aftername2){
handle = array1[j];
array1[j] = array1[j+1];
array1[j+1] = handle;//swapping
}
if(aftername1 == aftername2){
if(forname1 > forname2){
handle = array1[j];
array1[j] = array1[j+1];
array1[j+1] = handle;
}
}
Run Code Online (Sandbox Code Playgroud)
一旦你将unicode字符投入混合,你就必须开始考虑国际化.不同的语言有不同的排序规则.例如,在荷兰语中,"IJ"被认为是单个字母,并且在字母表中有自己的位置.我推荐一个很好的Unicode库来进行字符串词汇比较,即Unicode的国际组件:http://site.icu-project.org/
有了它,你可以简单地使用普通std::sort的ICU比较器.
表格和转换。
我首先将字符串转换为全部大写或全部小写:
#include <cctype>
#include <algorithm>
#include <string>
std::string test_string("mR BroWn");
std::transform(test_string.begin(), test_string.end(),
test_string.begin(),
std::tolower);
Run Code Online (Sandbox Code Playgroud)
接下来,我将检查异常情况或使用等效表。如果相关字符位于异常字符数组中,则使用等效表。