Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5992401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:31:34+00:00 2026-05-22T23:31:34+00:00

I’m using Boost.Filesystem to create a listing of files in a directory. I use

  • 0

I’m using Boost.Filesystem to create a listing of files in a directory. I use boost::filesystem::recursive_directory_iterator and std::copy to put each path into a std::vector as a boost::filesystem::directory_entry object. I wish to output to the file as std::strings though, so I did the following (\n to avoid the use of <<):

std::vector<boost::filesystem::directory_entry> buffer; //filled with paths
...
std::vector<std::string> buffer_native(buffer.size());
//transform directory_entry into std::string, and add a \n, so output is formatted without use of <<
std::transform(buffer.begin(),buffer.end(),buffer_native.begin(), [](boost::filesystem::directory_entry de)->std::string
    {
        std::string temp=de.path().string();
        temp+="\n";
        return temp;
    }
    buffer.clear();
    std::copy(buffer_native.begin(),buffer_native.end(),std::ostream_iterator<std::string>(out_file));

However the problem with this is it creates two vectors, the original of which is immediately cleared because it is not needed. This sounds like a perfect place for move semantics, but n3242 only provides the same two overloads of transform as in C++98. Is it possible to implement move semantics with std::transform? If it is not, would writing a custom loop be better?

I’m using GCC 4.5.2 (MinGW) on Windows XP.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:31:35+00:00Added an answer on May 22, 2026 at 11:31 pm

    This looks like a job for make_move_iterator:

    std::transform(make_move_iterator(buffer.begin()),
                    make_move_iterator(buffer.end()), buffer_native.begin(),
                    [](boost::filesystem::directory_entry&& de) -> std::string
    {
        // still makes copy :/ perhaps native() would work better, I don't know
        std::string temp = de.path().string();
        temp += "\n";
    
        return temp;
    }
    

    A move iterator is simply an iterator which moves its dereference result. Note that the class needs to support move semantics for this to even make a difference; I don’t know if Boost FS does.


    Note if your goal is to output them on separate lines, you’re doing it wrong. Formatted printing shouldn’t require the input data be in a certain format, that defeats the purpose. Adding newlines to your data just to format it to have newlines is nasty. It’s handled for you by ostream_iterator anyway:

    std::copy(buffer.begin(), buffer.end(), //               vvvv
                std::ostream_iterator<std::string>(out_file, "\n"));
    

    Anything more complex, make a lambda in for printing; don’t modify your data beforehand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.