对std :: string中的数字排序? [英] Sorting std::strings with numbers in them?

查看:812
本文介绍了对std :: string中的数字排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在按std :: string<运算符。其问题是:



30< 30显示在9之前, 9,Windows 9x有这个问题。我怎么能对它们进行数字排序,使30狐狸出现在9只狗之后。我还要补充一点,我使用的是utf 8编码。



感谢

解决方案>

您可以创建一个自定义比较函数,用于 std :: sort 。此函数必须检查字符串是否以数字值开头。如果是这样,使用一些机制,如stringstream将每个字符串的数字部分转换为 int 。然后比较两个整数值。如果值相等,则按字典顺序比较字符串的非数字部分。



基本上,类似下面的(未测试的)比较函数:

/ p>

  bool is_not_digit(char c)
{
return!std :: isdigit(c);
}

bool numeric_string_compare(const std :: string& s1,const std :: string& s2)
{
//处理空字符串...

std :: string :: const_iterator it1 = s1.begin(),it2 = s2.begin();

if(std :: isdigit(s1 [0])&& std :: isdigit(s2 [0])){
int n1,n2;
std :: stringstream ss(s1);
ss>> n1;
ss.clear();
ss.str(s2);
ss>> n2;

if(n1!= n2)return n1< n2;

it1 = std :: find_if(s1.begin(),s1.end(),is_not_digit);
it2 = std :: find_if(s2.begin(),s2.end(),is_not_digit);
}

return std :: lexicographical_compare(it1,s1.end(),it2,s2.end());
}

然后...

  std :: sort(string_array.begin(),string_array.end(),numeric_string_compare); 

编辑:当然,这个算法只有在你排序数字部分出现的字符串在字符串的开头。如果你正在处理字符串中数字部分可以出现在字符串中的任何地方,那么你需要一个更复杂的算法。有关详情,请参见 http://www.davekoelle.com/alphanum.html


I'm currently sorting by the std::string < operator. The problem with it is that:

30 < 9. The 30 shows up before the 9 since 3 < 9, Windows 9x had this issue to. How could I go about sorting them numerically so that "30 Foxes" shows up after "9 dogs". I should also add that I'm using utf 8 encoding.

Thanks

解决方案

You can create a custom comparison function to use with std::sort. This function would have to check if the string begins with a numeric value. If it does, convert the numeric part of each string to an int using some mechanism like a stringstream. Then compare the two integer values. If the values compare equally, compare the non-numeric part of the strings lexicographically. Otherwise, if the strings don't contain a numeric part, simply compare the two strings lexicographically as normal.

Basically, something like the following (untested) comparison function:

bool is_not_digit(char c)
{
    return !std::isdigit(c);
}

bool numeric_string_compare(const std::string& s1, const std::string& s2)
{
    // handle empty strings...

    std::string::const_iterator it1 = s1.begin(), it2 = s2.begin();

    if (std::isdigit(s1[0]) && std::isdigit(s2[0])) {
        int n1, n2;
        std::stringstream ss(s1);
        ss >> n1;
        ss.clear();
        ss.str(s2);
        ss >> n2;

        if (n1 != n2) return n1 < n2;

        it1 = std::find_if(s1.begin(), s1.end(), is_not_digit);
        it2 = std::find_if(s2.begin(), s2.end(), is_not_digit);
    }

    return std::lexicographical_compare(it1, s1.end(), it2, s2.end());
}

And then...

std::sort(string_array.begin(), string_array.end(), numeric_string_compare);

EDIT: Of course, this algorithm is only useful if you're sorting strings where the numeric portion appears at the beginning of the string. If you're dealing with strings where the numeric portion can appear anywhere in the string, then you need a more sophisticated algorithm. See http://www.davekoelle.com/alphanum.html for more information.

这篇关于对std :: string中的数字排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆