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 8538769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:12:00+00:00 2026-06-11T11:12:00+00:00

I have to send the same string (e.g. a log message) to multiple streams.

  • 0

I have to send the same string (e.g. a log message) to multiple streams.
Which of the following solutions is the most efficient?

  1. Rebuild the same string for each stream and send it to the stream itself.

    outstr1 << "abc" << 123 << 1.23 << "def" << endl;  
    outstr2 << "abc" << 123 << 1.23 << "def" << endl;  
    outstr3 << "abc" << 123 << 1.23 << "def" << endl;  
    
  2. Build the string once with string’s operators, and send it to all the streams.

    std::string str = "abc" + std::to_string(123) + std::to_string(1.23) + "def";  
    outstr1 << str;  
    outstr2 << str;  
    outstr3 << str;  
    
  3. Build the string once with a stream, and send it to all the streams:

    std::stringstream sstm;  
    sstm << "abc" << 123 << 1.23 << "def" << endl;  
    std::string str = sstm.str();  
    outstr1 << str;  
    outstr2 << str;  
    outstr3 << str;  
    

Some or all of these output streams could be on a RAM disk.

Any other ways to do the same thing?

  • 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-06-11T11:12:01+00:00Added an answer on June 11, 2026 at 11:12 am

    The “proper” way to do something like this is to have a stream buffer writing to multiple destinations and use this stream buffer via a std::ostream. This way the code looks as if it writing just once but the characters are sent multiple times. Searching for “teebuf Dietmar” will find a few variations on the same theme.

    To also comment on your question: Which one of the three alternatives is the fastest depends on the exact expressions you have:

    1. needs to evaluate the involved expressions and perform the conversions three times. Depending on what you actually do this may still be fairly fast.
    2. actually creates and destroys multiple streams and does multiple allocations for std::string. I’d expect this to be slowest.
    3. still creates a stream (which should actually be a std::ostringstream) and allocates some memory. Out of your options I’d expect it to be fastest.

    Using a teebuf is probably fastest, at least, when it does some buffering but uses only fixed suze arrays, both for the buffer and the array of stream buffer pointers. Note, that you’ll need to override sync() to deal with the buffer in a timely manner, though.

    To determine the actual performance you’ll need to measure!

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

Sidebar

Related Questions

Trying to send a complex type between two systems that have the same code
I have to send a long string of data through webservice to database.I odnt
Here I have to send and receive dynamic data using a SysV message queue.
I am trying to send the same parameter name with multiple values, but even
Scenario: In the database I have a field called Categories which of type string
I want to have access to the same message that Powershell prints when you
Say I have an NSSet with a string in it. If I send containsObject:
I have to send the following data to webservice at URl The format of
I have an app which works with UDP packets. So I want to send
I have the following string: char *str = \x45\x00\x10; I need to encrypt it

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.