首页 > 解决方案 > VS2019 上的 c++ 目录迭代器异常-已更改语言设置,还有什么要尝试的,2019 年是否已更改?

问题描述

我从 2017 年开始迁移到 vs2019,它带来了一些小而烦人的挑战......

其中之一是我为遍历目录并将文件路径推送到数组中而开发的这段代码。

我已将语言设置更新为属性页上的最新草稿,允许我删除命名空间中的::experimental。

代码会构建,但在调试和单步执行时,当迭代器尝试初始化到目录路径时会引发异常。

我错过了什么,因为几天前在 vs2017 上工作正常。

截屏!

我检查了图书馆,我想我正在提供它所要求的东西?

图书馆

标签: c++iteratorboost-filesystem

解决方案


如果有效,您能否在您的机器上检查以下内容和反馈(通过评论)?

#include <iostream>
#include <filesystem>
#include <vector>
#include <iterator>

namespace fs = std::filesystem;

int main() {

    // The start path
    const fs::path startPath{ "C:\\temp\\" };

    std::vector<std::string> files{};

    // Get all path names as string
    std::transform(fs::directory_iterator(startPath), {}, std::back_inserter(files), [](const fs::directory_entry& de) { return de.path().string(); });

    // Output all files
    std::copy(files.begin(), files.end(), std::ostream_iterator<std::string>(std::cout, "\n"));

    return 0;
}

因为这可以在我的机器上使用 Microsoft Visual Studio Community 2019 版本 16.5.2


推荐阅读