我目前正在尝试对向量中的字符串进行排序,但是我不确定如何以特定方式进行操作。我正在使用的游戏引擎要求字符串不区分大小写地排序(我已经能够做到)。它还用字母前的下划线对这些文件名进行排序。
例如,这些字符串当前的排序如下:
ChallengeBattleships.bsf
ChallengeBattleships.zip
ChallengeBattleshipsPause.bsf
ChallengeBattleshipsSummary.bsf
ChallengeBattleshipsSurface.bsf
ChallengeBattleships_arena.bsf
ChallengeBattleships_day.zip
但是我需要对它们进行这样的排序(以及不区分大小写):
ChallengeBattleships.bsf
ChallengeBattleships.zip
ChallengeBattleships_arena.bsf
ChallengeBattleships_day.zip
ChallengeBattleshipsPause.bsf
ChallengeBattleshipsSummary.bsf
ChallengeBattleshipsSurface.bsf
会有人碰巧知道如何以这种方式对向量中的字符串进行排序吗?
将std :: sort与lambda一起使用。如果原始字符串不被修改,则:
std::sort(vec.begin(), vec.end(),
[](std::string s1, std::string s2){
std::transform(s1.begin(), s1.end(), s1.begin(),
[](char c){ return std::tolower(c); });
std::transform(s2.begin(), s2.end(), s2.begin(),
[](char c){ return std::tolower(c); });
return s1 < s2;
});
更改大小写:如何将std :: string转换为小写?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句