В настоящее время я ищу аккуратные способы, которыми я могу перебрать этот вектор кортежей ..
Это то, чем я сейчас занимаюсь?
#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;
}
Несмотря на то, что он выполняет свою работу, он не кажется «дружественным к читателю», есть ли другие способы, с помощью которых я могу перебирать элементы вектора более удобным для читателя способом?
Я попытался сделать цикл 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 ++ 11 вы можете сделать
for (auto const& tup : items)
std::experimental::filesystem::path path;
std::tie(path, std::ignore, std::ignore) = tup;
std::cout << path << std::endl;
}