下面,我产生了破碎的代码和相同的固定版本。问题是我不能完全向自己解释为什么前者不起作用,但后者却起作用。我显然需要回顾一下C++语言的一些非常基本的概念:能否提供我应该查看的内容的指针,并且可能还会解释为什么我会用破碎的代码得到结果。基本概念与std ::字符串引用,std :: regex和boost :: filesystem
在代码中引用的'../docs/'目录中,我只是在linux上使用'touch'命令来创建大量不同长度的doc ...... html文件。
#include <iostream>
#include <regex>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main() {
fs::path p("../docs/");
for (auto& dir_it : fs::directory_iterator(p)) {
std::regex re = std::regex("^(doc[a-z]+)\\.html$");
std::smatch matches;
// BROKEN HERE:
if (std::regex_match(dir_it.path().filename().string(), matches, re)) {
std::cout << "\t\t" <<dir_it.path().filename().string();
std::cout << "\t\t\t" << matches[1] << std::endl;
}
}
return 0;
}
产地:
documentati.html ati
documentationt.html �}:ationt
document.html document
documenta.html documenta
docume.html docume
documentat.html documentat
docum.html docum
documentatio.html ��:atio
documen.html documen
docu.html docu
documentation.html ��:ation
documaeuaoeu.html ��:aoeu
注1:错误上述被触发以文件名,它们是一定长度以上。我只理解这是因为std :: string对象正在调整其自身。
注2:上面的代码比在以下问题中使用的代码非常相似,但的boost :: regex_match来代替std :: regex_match: Can I use a mask to iterate files in a directory with Boost?
它使用之前,以及对我的工作,但现在我使用GCC 5.4而不是GCC 4.6,std :: regex代替boost :: regex,C++ 11,以及更新版本的boost :: filesystem。哪些变化是相关的,导致工作代码被破坏?
修正:
#include <iostream>
#include <regex>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main() {
fs::path p("../docs/");
for (auto& dir_it : fs::directory_iterator(p)) {
std::regex re = std::regex("^(doc[a-z]+)\\.html$");
std::smatch matches;
std::string p = dir_it.path().filename().string();
if (std::regex_match(p, matches, re)) {
std::cout << "\t\t" <<dir_it.path().filename().string();
std::cout << "\t\t\t" << matches[1] << std::endl;
}
}
return 0;
}
生产:
documentati.html documentati
documentationt.html documentationt
document.html document
documenta.html documenta
docume.html docume
documentat.html documentat
docum.html docum
documentatio.html documentatio
documen.html documen
docu.html docu
documentation.html documentation
documaeuaoeu.html documaeuaoeu
使用升压1.62.0-R1和gcc(Gentoo的5.4.0-R3),升压::文件系统文件不会出现()。filename()。string()返回: http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#path-filename
看起来它取决于:
Why does boost::filesystem::path::string() return by value on Windows and by reference on POSIX?
哦!现在我明白了,看起来很明显。 +1。谢谢你的时间。 – augustin