std::string&

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 ?

thanks in advance.
> understand std::string& and why they add & after std::string ?

See: http://www.parashift.com/c++-faq-lite/overview-refs.html
Can you help me to understand this code ? and how to execute it ?
thanks
> Can you help me to understand this code ?

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <boost/filesystem.hpp>
#include <vector>
#include <string>

// return a list of all files in directory 'target_path' with extensionb 'ext'
std::vector<std::string> enumerate_files( const std::string& target_path,
                                          const std::string& ext )
{
    std::vector<std::string> result ;

    using iterator = boost::filesystem::directory_iterator ;
    using boost::filesystem::is_regular_file ;

    try
    {
        for( iterator iter(target_path); iter != iterator() ; ++iter )
        {
            if( is_regular_file(*iter) && iter->path().extension() == ext )
                      result.push_back( iter->path().filename().string() );
        }
    }
    catch( const std::exception& ) { /* ... */ }

    return result ;
}


This is how you would use it; that part is easy enough:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    const std::string directory =  "/usr/local/include" ; // "C:\\windows\\system32" ;
    const std::string extension =  ".h" ;                 // ".dll" ;

    std::vector<std::string> files = enumerate_files( directory, extension ) ;

    std::cout << "directory: '" << directory << "' has " << files.size()
              << " files with extension '" << extension << "'\n" ;

    for( std::size_t i = 0 ; i < files.size() ; ++i )
        std::cout << "    " << i+1 << ". " << files[i] << '\n' ;
}


Note: To be able to run this, you should have boost and you must link with the boost filesystem library.
Topic archived. No new replies allowed.