编辑:我根据Jonathan的回答在我的问题下面添加了一些解决方案
我希望在给定目录中有一个具有特定名称模式的常规文件列表。我从boost.filesystem(boost 1.53)中选取了一个示例并对其进行了修改。这是我基本上想要的工作版本:
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
using namespace std;
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
path p ("."); // search directory
try
{
if (exists(p))
{
if (is_directory(p))
{
cout << "logfiles in " << absolute(p) << '\n';
// I want to replace this part:
std::list<directory_entry> datFiles;
for(directory_iterator it(p); it != directory_iterator(); ++it)
{
if (is_regular_file(*it) && (extension(*it) == ".dat"))
{
datFiles.push_back(*it);
}
}
// part to be replaced ends here
BOOST_FOREACH(const directory_entry& de, datFiles)
{
cout << de << '\n';
}
}
}
}
catch (const filesystem_error& ex)
{
cout << ex.what() << '\n';
}
return 0;
}
这会将以.dat
结尾的所有文件的条目附加到目录条目列表中。我以后需要更多模式匹配。然后我尝试用范围做同样的事情,但失败了。我现在将任务简化为简单地复制所有不编译的目录条目:
// replacement starts here
std::list<directory_entry> datFiles;
boost::iterator_range<directory_iterator> rng(directory_iterator(p), directory_iterator());should be
boost::copy(rng, datFiles.begin());
// replacement ends here
我是这样编写的,因为(来自boost.filesystem的tut3.cpp)
directory_iterator :: value_type是directory_entry
因此,副本可以遍历范围,获取条目并将其存储在我的列表中。但是,我收到了很多来自gcc的错误消息:
In file included from boost_1_53_0/boost/type_traits/decay.hpp:18:0,
from boost_1_53_0/boost/filesystem/path_traits.hpp:22,
from boost_1_53_0/boost/filesystem/path.hpp:25,
from boost_1_53_0/boost/filesystem.hpp:16,
from ~/20130519_searchDatFiles/main.cpp:3:
boost_1_53_0/boost/mpl/eval_if.hpp: In instantiation of ‘boost::mpl::eval_if_c<false, boost::range_const_iterator<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())>, boost::range_mutable_iterator<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())> >’:
boost_1_53_0/boost/range/iterator.hpp:63:63: instantiated from ‘boost::range_iterator<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())>’
boost_1_53_0/boost/range/concepts.hpp:256:72: instantiated from ‘boost::SinglePassRangeConcept<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())>’
boost_1_53_0/boost/concept/detail/has_constraints.hpp:42:5: instantiated from ‘const bool boost::concepts::not_satisfied<boost::SinglePassRangeConcept<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())> >::value’
boost_1_53_0/boost/concept/detail/has_constraints.hpp:45:31: instantiated from ‘boost::concepts::not_satisfied<boost::SinglePassRangeConcept<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())> >’
boost_1_53_0/boost/mpl/if.hpp:67:11: instantiated from ‘boost::mpl::if_<boost::concepts::not_satisfied<boost::SinglePassRangeConcept<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())> >, boost::concepts::constraint<boost::SinglePassRangeConcept<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())> >, boost::concepts::requirement<boost::concepts::failed************ boost::SinglePassRangeConcept<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())>::************> >’
boost_1_53_0/boost/concept/detail/general.hpp:50:8: instantiated from ‘boost::concepts::requirement_<void (*)(boost::SinglePassRangeConcept<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())>)>’
boost_1_53_0/boost/range/algorithm/copy.hpp:33:1: instantiated from ‘OutputIterator boost::range::copy(const SinglePassRange&, OutputIterator) [with SinglePassRange = boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)()), OutputIterator = std::_List_iterator<boost::filesystem::directory_entry>]’
~/20130519_searchDatFiles/main.cpp:45:42: instantiated from here
boost_1_53_0/boost/mpl/eval_if.hpp:60:31: error: no type named ‘type’ in ‘boost::mpl::eval_if_c<false, boost::range_const_iterator<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())>, boost::range_mutable_iterator<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())> >::f_ {aka struct boost::range_mutable_iterator<boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)())>}’
In file included from boost_1_53_0/boost/range/algorithm.hpp:53:0,
from ~/20130519_searchDatFiles/main.cpp:7:
boost_1_53_0/boost/range/algorithm/copy.hpp: In function ‘OutputIterator boost::range::copy(const SinglePassRange&, OutputIterator) [with SinglePassRange = boost::iterator_range<boost::filesystem::directory_iterator>(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)()), OutputIterator = std::_List_iterator<boost::filesystem::directory_entry>]’:
~/20130519_searchDatFiles/main.cpp:45:42: instantiated from here
boost_1_53_0/boost/range/algorithm/copy.hpp:34:59: error: no matching function for call to ‘begin(boost::iterator_range<boost::filesystem::directory_iterator> (&)(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)()))’
boost_1_53_0/boost/range/algorithm/copy.hpp:34:59: note: candidates are:
boost_1_53_0/boost/range/begin.hpp:101:55: note: template<class T> typename boost::range_iterator::type boost::range_adl_barrier::begin(T&)
boost_1_53_0/boost/range/begin.hpp:112:61: note: template<class T> typename boost::range_iterator<const T>::type boost::range_adl_barrier::begin(const T&)
boost_1_53_0/boost/range/algorithm/copy.hpp:34:59: error: no matching function for call to ‘end(boost::iterator_range<boost::filesystem::directory_iterator> (&)(boost::filesystem::directory_iterator, boost::filesystem::directory_iterator (*)()))’
boost_1_53_0/boost/range/algorithm/copy.hpp:34:59: note: candidates are:
boost_1_53_0/boost/range/end.hpp:95:55: note: template<class T> typename boost::range_iterator::type boost::range_adl_barrier::end(T&)
boost_1_53_0/boost/range/end.hpp:106:61: note: template<class T> typename boost::range_iterator<const T>::type boost::range_adl_barrier::end(const T&)
boost_1_53_0/boost/range/algorithm/copy.hpp:35:1: warning: control reaches end of non-void function [-Wreturn-type]
我真的无法过滤掉相关的行,但我认为可以使用范围适配器过滤文件(毕竟我不只是想复制范围)。我的印象是,datFiles.begin()
的某个地方不能满足复制算法所需的概念。
根据Jonathan的回答,我提出了一些解决方案(包括过滤)。
使用全局谓词函数bool isDatFile(const path& p)
:
std::list<directory_entry> datFiles;
boost::copy(rng | boost::adaptors::filtered(isDatFile), std::back_inserter(datFiles));
我更喜欢本地功能,所以我试过了。这个只是检查目录条目是否是常规文件,因为is_regular_file
被重载需要静态强制转换:
std::list<directory_entry> datFiles;
boost::copy(rng
| boost::adaptors::filtered(static_cast<bool (*)(const path&)>(&is_regular_file)),
std::back_inserter(datFiles));
在范围适配器中检查正确的扩展名也有点困难,所以我添加了一个本地函数:
std::list<directory_entry> datFiles;
bool BOOST_LOCAL_FUNCTION(const path& p)
{
return (is_regular_file(p) && (extension(p) == string(".dat")));
} BOOST_LOCAL_FUNCTION_NAME(isDatFile_l)
boost::copy(rng
| boost::adaptors::filtered(isDatFile_l),
std::back_inserter(datFiles));
最后使用bind的“one-liner”:
std::list<directory_entry> datFiles;
boost::copy(rng
| boost::adaptors::filtered(static_cast<bool (*)(const path&)>(&is_regular_file))
| boost::adaptors::filtered(boost::bind(std::equal_to<path>(), boost::bind(extension, _1), path(".dat"))),
std::back_inserter(datFiles));
答案 0 :(得分:8)
此行正在点击Most Vexing Parse:
boost::iterator_range<directory_iterator> rng(directory_iterator(p), directory_iterator());
声明函数不是对象。
您可以将其更改为:
boost::iterator_range<directory_iterator> rng = boost::iterator_range<directory_iterator>(directory_iterator(p), directory_iterator());
或者在C ++ 11中:
auto rng = boost::iterator_range<directory_iterator>(directory_iterator(p), directory_iterator());
但是使用工厂功能更好:
boost::iterator_range<directory_iterator> rng = boost::make_iterator_range(directory_iterator(p), directory_iterator());
通过使用make_iterator_range
,您不需要命名迭代器的类型,它将从参数类型中推断出来。
另一个问题是你正在写一个空列表,这是一个未定义的行为,你应该使用一个插入迭代器来调用你工作代码中的push_back()
。把它们放在一起:
std::list<directory_entry> datFiles;
boost::copy( boost::make_iterator_range(directory_iterator(p), directory_iterator()),
std::back_inserter(datFiles) );
在C ++ 11中,它更简洁:
std::list<directory_entry> datFiles;
boost::copy( boost::make_iterator_range(directory_iterator(p), {}),
std::back_inserter(datFiles) );
现在您可以非常轻松地添加过滤器:
std::list<directory_entry> datFiles;
boost::copy( boost::make_iterator_range(directory_iterator(p), {}) | some_dir_filter,
std::back_inserter(datFiles) );