I am using the same example as in the boost tutorials. But since my file names are numbered (1,20,23,..). The code fails to compare the strings(ex. 20 < 7). Is there a way to compare the directory_iteration numerically. Here is a snippet of the code
else if (is_directory(p)) // is p a directory?
{
cout << p << " is a directory containing:\n";
typedef vector<path> vec; // store paths,
vec v; // so we can sort them later
copy(directory_iterator(p), directory_iterator(), back_inserter(v));
sort(v.begin(), v.end()); // **I want to sort this numerically**
for (vec::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it)
{
cout << " " << *it << '\n';
}
}
The layout of Directory and subfolder is shown here:
root/
1/
1.bmp
2.bmp
3.bmp
4.bmp
...
2/
1.bmp
2.bmp
3.bmp
4.bmp
....
3/
1.bmp
2.bmp
3.bmp
4.bmp
....
Since you cannot change the format of the filenames to be sortable as is, you will need to do some processing yourself -- parse the number from each file name, and use that for sorting.
Two approaches come to mind, trading off between memory and CPU usage.
Approach 1:
Store pairs of filename and numerical value, parsing when populating the vector.
Approach 2:
Store just the paths and perform conversion during comparison.
Code:
Contents of the directory:
Output:
Updating it to handle the whole directory structure you've shown, you could have something like this:
Example: