提问人:Jean-Milost Reymond 提问时间:2/17/2022 更新时间:3/15/2022 访问量:51
是否可以根据复杂的标准使用 std::sort() 对字符串向量进行排序?
Is it possible to sort a vector of string with std::sort(), based on a complex criteria?
问:
我需要对包含文件夹名称的文件夹进行排序,使父文件夹始终位于其所有子文件夹之后,例如:std::vector<std::wstring>
C:\Main\App\QtGraphicalEffects\private
C:\Main\App\QtGraphicalEffects
C:\Main\App\Qt\labs\platform
C:\Main\App\Qt\labs
C:\Main\App\Qt
C:\Main\App
C:\Main\
为了达到这样的排序,我可以使用气泡排序算法,如下所示:
void BubbleSortDirs(std::vector<std::wstring>& dirs)
{
bool swapped = false;
do
{
swapped = false;
for (std::size_t i = 0; i < dirs.size() - 1; ++i)
// swap positions if first dir is entirely contained in the second
if (dirs[i] != dirs[i + 1] && dirs[i + 1].find(dirs[i]) == 0)
{
std::swap(dirs[i], dirs[i + 1]);
swapped = true;
}
}
while (swapped);
}
这段代码运行良好,但我觉得应该有更好的解决方案。因此,我尝试使用该功能来优化我的排序,至少提供更优雅的解决方案。std::sort
我尝试了以下实现:
bool SortDirs(const std::wstring& first, const std::wstring& second)
{
// swap positions if first dir is entirely contained in the second
return (first == second || first.find(second) == 0);
}
...
std::sort(dirs.begin(), dirs.end(), SortDirs);
我期望这将提供与函数相同的结果,但事实并非如此,结果在几个位置都失败了。在这一点上,我强烈怀疑它不适合像我试图应用的复杂排序标准。std::sort()
BubbleSortDirs()
std::sort()
所以我的问题是:
- 我的调用没有提供预期结果的原因是什么?
std::sort
- 有没有办法使用该函数实现上述排序?
std::sort()
- 如果是,我做错了什么?
- 如果不是,上述函数是实现这种排序的最佳方式,还是存在更好的东西?
BubbleSortDirs()
答:
0赞
Jean-Milost Reymond
3/15/2022
#1
我终于以这种方式解决了我的问题:
std::sort(dirs.rbegin(), dirs.rend());
评论