i'm new in c++, i have a function for enumerating files in a directory :
1 2 3 4 5 6 7 8 9 10 11
int EnumerateFiles(const std::string& target_path, const std::string& ext, std::vector<std::string>& res){
boost::filesystem::directory_iterator end_itr: // default ctor yields past-the-end
for(boost::filesystem::directory_iterator i(target_path); i != end_itr; ++it){
// skip if not a file
if( !boost::filesystem::is_regular_file( i->status() ) ) continue;
// skip if no match with extension
if( i->path().extension() != ext.c_str() ) continue;
// file matches, store it
res.push_back(i->path().filename().c_str());
}
}
i would like to understand this function and understand std::string& and why they add & after std::string ?
If you are new to C++, you will not be able to understand the code right now.
> and how to execute it ?
The good news is that you do not need to understand how a function is implemented to be able to use it.
What the function tries to do is return a list of files in a particular directory with a specified extension.
The code that you posted has errors; it won't even compile. So I've changed it into something that will work: