对文件系统路径向量进行排序时,“尝试取消引用过去的迭代器”

Mia*_*ami 2 c++ sorting dereference c++17 std-filesystem

我正在使用std :: filesystem编写一个简单的文件选择器。当前目录的条目存储在向量中。当我尝试使用std :: sort对向量进行排序时,程序崩溃。

在Ubuntu 19.04上使用g ++-9会发生这种情况。该文件是使用-D_GLIBCXX_DEBUG和-D_GLIBCXX_DEBUG_PEDANTIC调试标志编译的。

相关代码如下:

#include <filesystem>
#include <vector>
#include <algorithm>

namespace fs = std::filesystem;

struct FileBrowser {
    std::vector<fs::path> files;

    FileBrowser() {
        UpdateFiles();
    }

    void UpdateFiles() {
        files.clear();
        for (const auto& entry : fs::directory_iterator(currentPath)) {
            files.push_back(entry.path());
        }


        std::sort(files.begin(), files.end(), [](const auto& lhs, const auto& rhs) {
            if (fs::is_directory(lhs) && !fs::is_directory(rhs)) {
                return 1;
            } else if (fs::is_directory(rhs) && !fs::is_directory(lhs)) {
                return 0;
            } else {
                return lhs.filename().string().compare(rhs.filename().string());
            }
        });
    }
};
Run Code Online (Sandbox Code Playgroud)

错误消息如下所示:

/usr/include/c++/9/debug/safe_iterator.h:294:
In function:
    __gnu_debug::_Safe_iterator<_Iterator, _Sequence, _Category>::reference 
    __gnu_debug::_Safe_iterator<_Iterator, _Sequence, 
    _Category>::operator*() const [with _Iterator = 
    __gnu_cxx::__normal_iterator<std::filesystem::__cxx11::path*, 
    std::__cxx1998::vector<std::filesystem::__cxx11::path, 
    std::allocator<std::filesystem::__cxx11::path> > >; _Sequence = 
    std::__debug::vector<std::filesystem::__cxx11::path>; _Category = 
    std::forward_iterator_tag; __gnu_debug::_Safe_iterator<_Iterator, 
    _Sequence, _Category>::reference = std::filesystem::__cxx11::path&]

Error: attempt to dereference a past-the-end iterator.

Objects involved in the operation:
    iterator "this" @ 0x0x7fff6c67d9d0 {
      type = __gnu_cxx::__normal_iterator<std::filesystem::__cxx11::path*, std::__cxx1998::vector<std::filesystem::__cxx11::path, std::allocator<std::filesystem::__cxx11::path> > > (mutable iterator);
      state = past-the-end;
      references sequence with type 'std::__debug::vector<std::filesystem::__cxx11::path, std::allocator<std::filesystem::__cxx11::path> >' @ 0x0x55ca5b4536c0
    }
Run Code Online (Sandbox Code Playgroud)

我在网上看到很多例子,其中使用vector.end()调用std :: sort。我尝试过files.end() - 1并收到相同的错误消息。

Pio*_*cki 6

std::sort需要一个严格的弱排序比较器,如[alg.sorting] / p3中所述

对于除[alg.binary.search]中所述算法以外的算法,comp应在值上引入严格的弱排序。

严格的弱排序比较器true仅在左侧操作数位于右侧操作数之前,false否则应返回。

std::basic_string::compare函数来编码其结果作为<00>0,如果字符串分别是字典顺序以下,比参数表达式等于或大于,。这样可以一次确定其参数之间的相互关系。但是,正值和负值都可以隐式转换为true布尔值,因此任何标准库算法都将错误地解释结果,从而导致不确定的行为。为了避免这种情况,std::sort函数调用可能如下所示:

std::sort(files.begin(), files.end(), [](const auto& lhs, const auto& rhs) {
    if (fs::is_directory(lhs) && !fs::is_directory(rhs)) {
        return true;
    } else if (fs::is_directory(rhs) && !fs::is_directory(lhs)) {
        return false;
    } else {
        return lhs.filename().string() < rhs.filename().string();
        //                            ~^~
    }
});
Run Code Online (Sandbox Code Playgroud)