不使用默认算法排序字符串的std向量 [英] sorting std vector of strings without using default algorithm
问题描述
我有 std :: vector
的 std :: strings
,每个都是一个文件名。假设文件名的格式为 some_name_n.xyz
。
I have an std::vector
of std::strings
, each of which is a filename. Suppose filenames are of the format some_name_n.xyz
.
问题是 some_name_10.xyz
小于 some_name_2.xyz
。这些文件是由其他进程生成的。
The problem is that some_name_10.xyz
is less than some_name_2.xyz
. The files are produced by some other process.
将_之后的数字考虑作为比较,而不仅仅是其长度,最不痛苦的方式是什么?
What is the least painful way to sort them so that the number after '_' is considered for comparison, and not just its length?
推荐答案
std :: sort
允许您指定二进制函数比较两个元素: http://www.cplusplus.com/reference/algorithm/sort/
std::sort
allows you to specify a binary function for comparing two elements: http://www.cplusplus.com/reference/algorithm/sort/
现在这只是构建二进制函数的问题。部分示例如下:使用数字对std :: string进行排序?
Now it's just a matter of constructing that binary function. A partial example is here: Sorting std::strings with numbers in them?
这篇关于不使用默认算法排序字符串的std向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!