在末尾按数字对文件名列表进行排序
Sort a list of file names by number at end
我有一个文件列表:
foo10.tif
foo2.tif
...
foo102.tif
我想根据文件名末尾的数字对它们进行排序。
最好使用 c++11 和 lambda。
这是有效的。 如果您有任何问题,请告诉我:
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector<string> aFiles =
{
"foo10.tif",
"foo2.tif",
"foo102.tif",
};
sort(begin(aFiles), end(aFiles), [](const string& a, const string& b)
{
auto RemoveExtension = [](const string& s) -> string
{
string::size_type pos = s.find_last_of('.');
if (pos != string::npos)
return s.substr(0, pos);
return s;
};
string partA = RemoveExtension(a), partB = RemoveExtension(b);
if (partA.length() != partB.length())
return partA.length() < partB.length();
return partA < partB;
} );
for (string s : aFiles)
cout << s << endl;
}
相关文章:
- 试图在c++中对数字列表进行排序
- 在STL - C++中按成绩对学生列表进行排序?
- 使用列表 STL 递归进行插入排序
- 列表 iter 不取消引用 使用列表进行插入排序
- 如何在 c++ 中根据第二个元素按降序对列表进行排序
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 如何按键对 std::对列表进行排序?
- 如何通过保持第一个插入的位置来对列表中插入的其余元素进行排序?
- 如何使用cpp编写选择排序算法以降序对元素列表进行排序?
- 使用自定义比较函数在类内进行列表排序
- 列表::排序更改值C
- 对于列表排序,有没有办法使用相同的参数让多个运算符重载?
- 在 c++ 中将数字从列表排序到数组中的气泡排序问题
- 运算符<用于列表排序
- C++列表排序功能
- C++ STL 算法(列表排序)OpenMP/多线程实现
- 列表排序不正确
- 列表排序和结构体向量排序之间的性能差距.c++
- 对抽象数据类型的stl::列表排序
- C++std列表排序使用自定义比较器,该比较器依赖于对象实例的成员变量