迭代元组向量 c+17 样式不起作用?
Iterating through a vector of tuples c+17 style don't work?
我目前正在寻找可以迭代这个元组向量的简洁方法..
这就是我目前正在做的事情?
#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;
}
尽管它完成了工作,但它似乎并不 "reader" 友好,有没有其他方法可以让我以更 reader 友好的方式迭代向量的元素方式?
我试图使 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;
}
我目前正在寻找可以迭代这个元组向量的简洁方法..
这就是我目前正在做的事情?
#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;
}
尽管它完成了工作,但它似乎并不 "reader" 友好,有没有其他方法可以让我以更 reader 友好的方式迭代向量的元素方式?
我试图使 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;
}