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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:36:53+00:00 2026-05-20T13:36:53+00:00

ostringstream s; s << "123"; cout << s.str().c_str() << endl; // how to clear

  • 0
ostringstream s;

s << "123";
cout << s.str().c_str() << endl;

// how to clear ostringstream here?
s << "456";
cout << s.str().c_str() << endl;

Output is:

123
123456

I need:

123
456

How can I reset ostringstream to get desired output?

  • 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-20T13:36:54+00:00Added an answer on May 20, 2026 at 1:36 pm
    s.str("");
    s.clear();
    

    The first line is required to reset the string to be empty; the second line is required to clear any error flags that may be set. If you know that no error flags are set or you don’t care about resetting them, then you don’t need to call clear().

    Usually it is easier, cleaner, and more straightforward (straightforwarder?) just to use a new std::ostringstream object instead of reusing an existing one, unless the code is used in a known performance hot spot.

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

Sidebar

Related Questions

I can do std::ostringstream oss; oss << 1; oss.str(); so why can't I do:
Is it possible to search the content of a std::ostringstream without using the std::ostringstream::str()
The input is string[] like below. "CSE111: CSE110 MATH101" "CSE110:" I need to order
I have a special type of ostringstream that I am trying to output text
I stumbled across this code. std::ostringstream str; /// (some usage) assert( ! str );
Given a function that takes an output stringstream as an argument: void Foo(const std::ostringstream&
std::ostringstream parmStream; char parmName[1024]; THTTPUrl::Encode(parmName, pParm->Name().c_str(), 1024); //I want to add the value of
my_macro << 1 << hello world << blah->getValue() << std::endl; should expand into: std::ostringstream
I have an issue regarding conversion from float to c++ string using ostringstream. Here
I read somewhere that snprintf is faster than ostringstream. Has anyone has any experiences

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.