且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我怎么算使用​​boost ::文件系统目录中的文件的数量?

更新时间:2023-11-29 13:38:16

下面是一个班轮在标准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;
}