c++17 filesystem, regex 遍历目录

linux 遍历目录+文件(优化版本)

c++17 FS 还是挺好用的,

VS2017支持,但是linux g++7.3 还是不支持 filesystem


#include<filesystem>
#include<regex>  //正则表达式

namespace fs = std::experimental::filesystem;


int main()
{
    string strPath = "D:\pic\new";
    regex fileSuffix("(.*)(.jpg)");// *.jpg, *.png  

    //regex fileSuffix("(.*).(.jpg)"); 也行
    //regex fileSuffix(".*z.*\.(jpg|png)");//包含字母z的所有jpg或png图片

    for (auto&DirectoryIter : fs::directory_iterator(strPath))
    {
    	auto filepath = DirectoryIter.path();
    	auto filename = filepath.filename();

	    if (std::regex_match(filename.string(), fileSuffix))
	    {
		vecFilePath.push_back(filepath.string());
		cout << filepath << endl;
	    }

	//replace_extension替换扩展名
	//stem去掉扩展名
    }
}

============

20190813
cpp20 也出来了, g++8.0 貌似也支持std::filesystem了

https://en.cppreference.com/w/cpp/filesystem

原文地址:https://www.cnblogs.com/scotth/p/9581734.html