2017-11-17 1 views
0

현재 튜플의 벡터를 반복 할 수있는 깔끔한 방법을 찾고 있습니다.튜플 c + 17 스타일의 벡터를 반복해도 작동하지 않습니까?

이것은 현재 무엇을하고 있습니까? 심지어는, 그것은 "독자"친절하기 위해 작업하지 않는 것 않습니다하지만

#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; 
} 

, 다른 방법이있는 I 수있는 반복 처리보다 독자 친화적 인 방법으로 벡터의 요소?

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 
+3

처음에는 "std :: experimental :: filesystem"이라고하는 긴 것을'사용 '하는 것이 어떻습니까? – gsamaras

+2

'namespace fs = std :: experimental :: filesystem;' – aschepler

+0

튜플 대신에 전용 구조체를 생성하면됩니다. 'std :: get 대신'index.path'를 사용하십시오. <0> (index)' – Jarod42

답변

5

C++ 17을

방법 중 하나는 내가 좀 더 읽을 같이함으로써이었다 for 루프를 만들려고 , 당신은 할 수 있습니다

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

실제로 시도해 보았습니다. 그러나 컴파일 오류로 끝나는 일부가 있습니까? https://pastebin.com/pnEYmNQU – Lamda

+0

해결 방법 : gcc를 v7.0으로 업그레이드하십시오. – Lamda

1

C++ 11에서 당신이 할 수

for (auto const& tup : items) 
    std::experimental::filesystem::path path; 
    std::tie(path, std::ignore, std::ignore) = tup; 
    std::cout << path << std::endl; 
}