提问人:desmond13 提问时间:2/7/2022 最后编辑:desmond13 更新时间:2/7/2022 访问量:91
按字母顺序对两对“std::p air<fs::p ath, fs::p ath>”排序
sort two pair `std::pair<fs::path, fs::path>` in alphabetical order
问:
我对C++相当陌生,我有两个文件夹,文件数量相同。我想按字母顺序对两个文件夹进行排序,然后打印filneame对:
[folder_1/file1.txt, folder_2/different_file4.txt]
[folder_1/file2.txt, folder_2/different_file9.txt]
[folder_1/file3.txt, folder_2/different_file35.txt]
...
这是我到目前为止编写的代码:
int main (int argc, char **argv)
{
cout << "C++ version: " << __cplusplus << "\n";
const fs::path path_1{ "/home/path_to_folder1" };
const fs::path path_2{ "/home/path_to_folder2" };
std::vector<std::pair<fs::path, fs::path>> v{};
std::transform(fs::recursive_directory_iterator{ path_1 }, {}, fs::recursive_directory_iterator{ path_2 }, std::back_inserter(v), [](auto& entry1, auto& entry2) {
return std::pair<fs::path, fs::path>{ entry1.path(), entry2.path() };
}
);
// TODO: I need to sort, since directory iteration is not ordered on some file systems
cout << "\n" ;
std:sort(v.begin(), v.end(), [](auto &left, auto &right) {
return left < right; // probably not needed for what I want (i.e., filenames sorted in alphabetical order)
cout << (left < right); // debug
});
for (auto& p : v)
{
std::cout << '[' << p.first << ", " << p.second << "]\n";
}
}
我知道我需要使用该功能,但我似乎无法让它工作。std::sort
编辑:
预期输出:
["/home/datasets/folder_1/file1.txt", "/home/datasets/folder_2/file432.txt"]
["/home/datasets/folder_1/file2.txt", "/home/datasets/folder_2/file456.txt"]
["/home/datasets/folder_1/file3.txt", "/home/datasets/folder_2/file556.txt"]
实际输出:
["/home/datasets/folder_1/file1.txt", "/home/datasets/folder_2/file556.txt"]
["/home/datasets/folder_1/file2.txt", "/home/datasets/folder_2/file432.txt"]
["/home/datasets/folder_1/file3.txt", "/home/datasets/folder_2/file456.txt"]
答: 暂无答案
评论
fs::path
operator<
std::pair
first
second
first
second
first
["/home/datasets/folder_1/file1.txt", "/home/datasets/folder_2/file432.txt"]