查找任何以word结尾的文件,c++
Find any file ending with word, c++
我想要等同于正则表达式'*bla.foo',意思是让我在 c++ 中以 bla.foo 结尾的任何文件。
到目前为止我想到的是:
#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp>
void get_all(const fs::path& root, const std::string& ext, std::vector<fs::path>& ret)
{
if(!fs::exists(root) || !fs::is_directory(root)) return;
fs::recursive_directory_iterator it(root);
fs::recursive_directory_iterator endit;
while(it != endit)
{
if(fs::is_regular_file(*it) && boost::algorithm::ends_with(it->path().filename(), ext)) ret.push_back(it->path().filename());
++it;
}
}
但我收到一个错误:
namespace boost {
namespace algorithm {
// is_equal functor -----------------------------------------------//
//! is_equal functor
/*!
Standard STL equal_to only handle comparison between arguments
of the same type. This is a less restrictive version which wraps operator ==.
*/
struct is_equal
{
//! Function operator
/*!
Compare two operands for equality
*/
template< typename T1, typename T2 >
bool operator()( const T1& Arg1, const T2& Arg2 ) const
{
return Arg1==Arg2;
}
};
错误是:
/usr/local/include/boost/algorithm/string/compare.hpp:43:28:
Invalid operands to binary expression ('const boost::filesystem::path' and 'int')
似乎传递给函数的参数有误,我想知道我哪里出错了。这是我在 C++ 中的第一个代码。
谢谢!
如果要比较字符串数据,需要在 path
对象上调用 string()
:
if (is_regular_file(*it) && boost::algorithm::ends_with(it->path().filename().string(), ext))
ret.push_back(it->path().filename());
我想要等同于正则表达式'*bla.foo',意思是让我在 c++ 中以 bla.foo 结尾的任何文件。
到目前为止我想到的是:
#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp>
void get_all(const fs::path& root, const std::string& ext, std::vector<fs::path>& ret)
{
if(!fs::exists(root) || !fs::is_directory(root)) return;
fs::recursive_directory_iterator it(root);
fs::recursive_directory_iterator endit;
while(it != endit)
{
if(fs::is_regular_file(*it) && boost::algorithm::ends_with(it->path().filename(), ext)) ret.push_back(it->path().filename());
++it;
}
}
但我收到一个错误:
namespace boost {
namespace algorithm {
// is_equal functor -----------------------------------------------//
//! is_equal functor
/*!
Standard STL equal_to only handle comparison between arguments
of the same type. This is a less restrictive version which wraps operator ==.
*/
struct is_equal
{
//! Function operator
/*!
Compare two operands for equality
*/
template< typename T1, typename T2 >
bool operator()( const T1& Arg1, const T2& Arg2 ) const
{
return Arg1==Arg2;
}
};
错误是:
/usr/local/include/boost/algorithm/string/compare.hpp:43:28:
Invalid operands to binary expression ('const boost::filesystem::path' and 'int')
似乎传递给函数的参数有误,我想知道我哪里出错了。这是我在 C++ 中的第一个代码。
谢谢!
如果要比较字符串数据,需要在 path
对象上调用 string()
:
if (is_regular_file(*it) && boost::algorithm::ends_with(it->path().filename().string(), ext))
ret.push_back(it->path().filename());