加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C模仿命令

发布时间:2020-12-16 09:27:48 所属栏目:百科 来源:网络整理
导读:如何实现ls“filename_ [0-5] [3-4]?”喜欢上课?结果我想存储在向量中. 目前我正在使用调用ls的system(),但这在MS下是不可移植的. 谢谢, 阿尔曼. 解决方法 以下程序列出当前目录中名称与正则表达式filename_ [0-5] [34]匹配的文件: #include boost/filesy
如何实现ls“filename_ [0-5] [3-4]?”喜欢上课?结果我想存储在向量中.

目前我正在使用调用ls的system(),但这在MS下是不可移植的.

谢谢,
阿尔曼.

解决方法

以下程序列出当前目录中名称与正则表达式filename_ [0-5] [34]匹配的文件:

#include <boost/filesystem.hpp>
#include <boost/regex.hpp>  // also functional,iostream,iterator,string
namespace bfs = boost::filesystem;

struct match : public std::unary_function<bfs::directory_entry,bool> {
    bool operator()(const bfs::directory_entry& d) const {
        const std::string pat("filename_[0-5][34]");
        std::string fn(d.filename());
        return boost::regex_match(fn.begin(),fn.end(),boost::regex(pat));
    }
};

int main(int argc,char* argv[])
{
    transform_if(bfs::directory_iterator("."),bfs::directory_iterator(),std::ostream_iterator<std::string>(std::cout,"n"),match(),mem_fun_ref(&bfs::directory_entry::filename));
    return 0;
}

为简洁起见,我省略了transform_if()的定义.它不是标准功能,但应该直接实现.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读