c++ - 迭代元组 vector c+17 样式不起作用?

标签 c++ for-loop c++17 code-readability structured-bindings

<分区>

我目前正在寻找可以迭代这个元组 vector 的简洁方法..

这就是我目前正在做的事情?

#include <experimental/filesystems>
#include <tuple>
#include <iostream>
#include <vector>


std::tuple<std::experimental::filesystem::path, std::experimental::filesystem::file_status, std::size_t>
file_info(const std::experimental::filesystem::directory_entry &entry)
{

    const std::experimental::filesystem::file_status fs(std::experimental::filesystem::status(entry));
    return {entry.path(),
                fs,
                std::experimental::filesystem::is_regular_file(fs) ? std::experimental::filesystem::file_size(entry.path()) : 0u};

}



int main ()
{
    std::experimental::filesystem::path _path(string_dir_to_test_files);  // string_dir_to_test_files is just a string 
    std::experimental::filesystem::directory_entry dir_path(_path);
    if (std::experimental::filesystem::exists(_path))
    {
        std::cout << "exists() = " << std::experimental::filesystem::exists(_path) << std::endl;
        std::cout << "Number of files in directory: " << number_of_files(_path) << std::endl;
        std::vector<std::tuple<std::experimental::filesystem::path,std::experimental::filesystem::file_status, std::size_t>> items;
        std::transform(std::experimental::filesystem::directory_iterator(_path),{},back_inserter(items),file_info);
        for( auto const& index : items)
        {
            std::cout << std::get<0>(index) << std::endl; // This line could be written better?
        }
    }
    else
    {
        std::cout << "No data to build database with!" << std::endl;
        exit(1);
    }

    return 0;
}

尽管它完成了工作,但它似乎对“读者”不友好,是否有其他方法可以让我以对读者更友好的方式迭代 vector 的元素?

我试图让 for 循环更具可读性的一种方法是像这样:

for (auto const& [path, status, size] : items) {
    std::cout << path << std::endl;
}

但这不知何故给了我这个错误:

/home/noob/soundcloud/src/include/database/database.cpp: In constructor ‘database::database()’:
/home/noob/soundcloud/src/include/database/database.cpp:31:25: error: expected unqualified-id before ‘[’ token
         for(auto const& [path, status, size] : items)
                         ^
/home/noob/soundcloud/src/include/database/database.cpp:31:25: error: expected ‘;’ before ‘[’ token
/home/noob/soundcloud/src/include/database/database.cpp:31:26: error: ‘path’ was not declared in this scope
         for(auto const& [path, status, size] : items)
                          ^~~~
/home/noob/soundcloud/src/include/database/database.cpp:31:26: note: suggested alternatives:
In file included from /usr/include/c++/6/experimental/filesystem:39:0,
                 from /home/noob/soundcloud/src/include/database/database.h:9,
                 from /home/noob/soundcloud/src/include/database/database.cpp:1:
/usr/include/c++/6/experimental/bits/fs_path.h:79:9: note:   ‘std::experimental::filesystem::v1::path’
   class path
         ^~~~
/usr/include/c++/6/experimental/bits/fs_path.h:79:9: note:   ‘std::experimental::filesystem::v1::path’
/home/noob/soundcloud/src/include/database/database.cpp:31:32: error: ‘status’ was not declared in this scope
         for(auto const& [path, status, size] : items)
                                ^~~~~~
/home/noob/soundcloud/src/include/database/database.cpp:31:32: note: suggested alternatives:
In file included from /usr/include/c++/6/experimental/filesystem:41:0,
                 from /home/noob/soundcloud/src/include/database/database.h:9,
                 from /home/noob/soundcloud/src/include/database/database.cpp:1:
/usr/include/c++/6/experimental/bits/fs_ops.h:274:15: note:   ‘std::experimental::filesystem::v1::status’
   file_status status(const path& __p, error_code& __ec) noexcept;
               ^~~~~~
/usr/include/c++/6/experimental/bits/fs_ops.h:274:15: note:   ‘std::experimental::filesystem::v1::status’
/home/noob/soundcloud/src/include/database/database.cpp:31:40: error: capture by copy of incomplete type ‘<unresolved overloaded function type>’
         for(auto const& [path, status, size] : items)
                                        ^~~~
/home/noob/soundcloud/src/include/database/database.cpp: In lambda function:
/home/noob/soundcloud/src/include/database/database.cpp:31:46: error: expected ‘{’ before ‘:’ token
         for(auto const& [path, status, size] : items)
                                              ^
/home/noob/soundcloud/src/include/database/database.cpp: In constructor ‘database::database()’:
/home/noob/soundcloud/src/include/database/database.cpp:31:46: error: expected ‘;’ before ‘:’ token
/home/noob/soundcloud/src/include/database/database.cpp:31:46: error: expected primary-expression before ‘:’ token
/home/noob/soundcloud/src/include/database/database.cpp:31:46: error: expected ‘)’ before ‘:’ token
/home/noob/soundcloud/src/include/database/database.cpp:31:46: error: expected primary-expression before ‘:’ token
make[2]: *** [src/include/database/CMakeFiles/database.dir/database.cpp.o] Error 1
make[1]: *** [src/include/database/CMakeFiles/database.dir/all] Error 2
make: *** [all] Error 2

最佳答案

在 C++17 中,你可以这样做:

for (auto const& [path, status, size] : items) {
    std::cout << path << std::endl;
}

关于c++ - 迭代元组 vector c+17 样式不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47350924/

相关文章:

c++ - 为什么 const 右值限定的 std::optional::value() 返回一个 const 右值引用?

c++ - 如何有条件地实例化具有多个模板参数的模板类?

c++ - 使用 Peterson 的 N 进程算法的信号量实现

c++ - 什么时候可以创建 'reserved' 标识符?

c# - 找不到 DLL : referencing dependent DLLs

逐行运行文件的python -regex匹配和for循环

python - Python 中嵌套 for 循环的向量化

c++ - 迭代快速排序方法的分区算法问题

python - 如何使用 pandas read (Python) 重命名数据框中的行?

c++ - 如何编写循环声明 C++14