用C自然排序目录文件名的++ [英] Natural Sort of Directory Filenames in C++
问题描述
我有我想要检索的文件名,并把它们串的载体中,使得他们在自然的方式排序的目录列表。例如 {10.txt0.txt,2.txt1.MJan12July13.txtNov25.txt简,约翰}
应 {0.txt1.M,2.txt10.txtJan12July13.txtNov25.txt简约翰}
。什么是做到这一点的最简单的方法?
在阐述自然的,我们承担的(N)和文本的(T)从数字的部分组成的字符串,使得。 ..(N)(T)...
,那么 ...(N1)(T1)...
和 ...(N2)(T2)...
将(N1< N2)(小于)(T1< T2)
其中,(小于)
意味着在正确的术语左项precedence。在这种情况下,数字需要precedence在文本字段,如果他们在字符串,即 1.Z在同一位置(小于)1_t.txt
<。 / p>
时已经有一个库函数做那种类型的字母数字字符串或目录条目?
所需的顺序在哪些文件应该来。文件名称将存储在字符串矢量
阿比纳夫@阿比纳夫-PC / cygdrive / C / AbhinavSamples /壳
$ LS -lv
共有8
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:51的1.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:55 1_t.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:50 3.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:51 4.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:53 10.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:56 10_t.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:56 13.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:53 20.txt**简单排序**
ABHI @ ABHI-PC / cygdrive / C / AbhinavSamples /壳
$ ls -l命令
共有8
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:51的1.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:53 10.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:56 10_t.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:56 13.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:55 1_t.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:53 20.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:50 3.txt
-rw-R - R的 - + 1阿比纳夫无17年3月2日00:51 4.txt
有就是做一个函数的正是你想要在glibc的东西。不幸的是C,而不是C ++,所以如果你能与住在这里是最简单可行的解决方案开箱即用,无需重新实现任何东西,另起炉灶。 BTW:这是完全一样的 LS -lv
实施。它的最重要的部分是 versionsort
函数确实在自然排序为您服务。它在这里被用作比较载体作用为 SCANDIR
。
下面这个简单的例子在打印如你所愿排列当前目录下的所有文件/目录。
的#define _GNU_SOURCE
#包括LT&;&dirent.h GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;INT主要(无效)
{
结构的dirent **名称列表;
诠释N,I; N = SCANDIR(,&AMP。名称列表,0,versionsort);
如果(正℃,)
PERROR(SCANDIR);
其他
{
对于(i = 0; I&LT; N ++ I)
{
的printf(%S \\ n,名称列表[I] - &GT; d_name);
免费(名称列表[我]);
}
免费(名称列表);
}
返回0;
}
I have a directory listing for which I want to retrieve the filenames and put them in a vector of strings such that they are sorted in a "natural" manner. e.g. { "10.txt" "0.txt" "2.txt" "1.m" "Jan12" "July13.txt" "Nov25.txt" "Jane" "John" }
should be {"0.txt" "1.m" "2.txt" "10.txt" "Jan12" "July13.txt" "Nov25.txt" "Jane" "John" }
. What is the easiest way to do this?
Elaborating on "natural" we assume for a string made up from parts of numbers (N) and text (T) such that ...(N)(T)...
, then for ...(N1)(T1)...
and ...(N2)(T2)...
will be (N1<N2) (<) (T1<T2)
where (<)
implies left term precedence over right term. In this case, numbers take precedence over text fields if they are in the same position in the string, i.e. 1.z (<) 1_t.txt
.
Is there already a library function to do that kind of sort for alphanumeric strings, or directory entries?
DESIRED order in which files should come. The file names will be stored in a vector of strings.
Abhinav@Abhinav-PC /cygdrive/c/AbhinavSamples/shell
$ ls -lv
total 8
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:51 1.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:55 1_t.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:50 3.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:51 4.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:53 10.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:56 10_t.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:56 13.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:53 20.txt
**Simple Sort**
Abhi@Abhi-PC /cygdrive/c/AbhinavSamples/shell
$ ls -l
total 8
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:51 1.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:53 10.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:56 10_t.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:56 13.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:55 1_t.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:53 20.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:50 3.txt
-rw-r--r--+ 1 Abhinav None 2 Mar 17 00:51 4.txt
There is a function that does exactly what you want in glibc. Unfortunately it is C, not C++, so if you can live with that here is the simplest possible solution "out of the box", without reimplementing anything and reinventing the wheel. BTW: this is exactly as ls -lv
is implemented. The most important part of it is the versionsort
function which does the natural sort for you. It is used here as a comparison functin for scandir
.
The simple example below prints all files/directories in current directory sorted as you wish.
#define _GNU_SOURCE
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
struct dirent **namelist;
int n,i;
n = scandir(".", &namelist, 0, versionsort);
if (n < 0)
perror("scandir");
else
{
for(i =0 ; i < n; ++i)
{
printf("%s\n", namelist[i]->d_name);
free(namelist[i]);
}
free(namelist);
}
return 0;
}
这篇关于用C自然排序目录文件名的++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!