gpt4 book ai didi

c++ - 目录迭代器值作为变量

转载 作者:搜寻专家 更新时间:2023-10-31 01:26:24 25 4
gpt4 key购买 nike

我正在尝试将 std:filesystem directory_iterator 的输出传输到 vector 或变量。不断出现错误:没有运算符“=”匹配这些操作数——操作数类型是:std::string = const std::filesystem

我真的是 C++ 的新手,在这一点上碰壁了。以前我一直在使用系统命令,但希望以后避免使用它。

#include <string>
#include <iostream>
#include <filesystem>
#include <vector>
namespace fs = std::filesystem;
using namespace std;
int main()
{
string objectInDirectory = "Jabberwocky";
string dirlisted = "";
for (const auto & entry : fs::directory_iterator(dirlisted)) {
cout << "Next directory entry: " << entry.path() << endl;
objectInDirectory = entry.path();
}
getchar();
}

最佳答案

entry.path() returns a const std::filesystem::path&。这不能隐式转换为 std::string,因此您需要调用它的字符串函数:

 objectInDirectory = entry.path().string();

不需要转换为string;您可以将其分配给 std::filesystem::path 而无需转换。这可能更理想,因为 std::filesystem::path 有比 std::string

更好的内置路径操作工具

关于c++ - 目录迭代器值作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55456585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com