我怎么算使用boost ::文件系统目录中的文件的数量? [英] How do I count the number of files in a directory using boost::filesystem?
本文介绍了我怎么算使用boost ::文件系统目录中的文件的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我给一个boost ::文件系统::路径。是否有一个快速的方式来获得的文件数在目录的路径指向?
I am given a boost::filesystem::path. Is there a fast way to get the number of files in the directory pointed to by the path?
推荐答案
下面是一个班轮在标准C ++:
Here's one-liner in Standard C++:
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/lambda/bind.hpp>
int main()
{
using namespace boost::filesystem;
using namespace boost::lambda;
path the_path( "/home/myhome" );
int cnt = std::count_if(
directory_iterator(the_path),
directory_iterator(),
bind( static_cast<bool(*)(const path&)>(is_regular_file),
bind( &directory_entry::path, _1 ) ) );
// a little explanation is required here,
// we need to use static_cast to specify which
// version of is_regular_file function we intend to use
std::cout << cnt << std::endl;
return 0;
}
这篇关于我怎么算使用boost ::文件系统目录中的文件的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文