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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:08:44+00:00 2026-06-14T21:08:44+00:00

Consider: std::string s_a, s_b; std::stringstream ss_1, ss_2; // at this stage: // ss_1 and

  • 0

Consider:

std::string        s_a, s_b;

std::stringstream  ss_1, ss_2;

// at this stage:
//     ss_1 and ss_2 have been used and are now in some strange state
//     s_a and s_b contain non-white space words

ss_1.str( std::string() );
ss_1.clear();

ss_1 << s_a;
ss_1 << s_b;

// ss_1.str().c_str() is now the concatenation of s_a and s_b, 
//                    <strike>with</strike> without space between them

ss_2.str( s_a );
ss_2.clear();

// ss_2.str().c_str() is now s_a

ss_2 << s_b;  // line ***

// ss_2.str().c_str() the value of s_a is over-written by s_b 
// 
// Replacing line *** above with "ss_2 << ss_2.str() << " " << s_b;"
//                    results in ss_2 having the same content as ss_1.

Questions:

  1. What is the difference between stringstream.str( a_value ); and
    stringstream << a_value; and, specifically, why does the first not
    allow concatenation via << but the second does?

  2. Why did ss_1 automatically get white-space between s_a and s_b, but
    do we need to explicitly add white space in the line that could
    replace line ***: ss_2 << ss_2.str() << " " << s_b;?

  • 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-14T21:08:45+00:00Added an answer on June 14, 2026 at 9:08 pm

    The problem you’re experiencing is because std::stringstream is constructed by default with ios_base::openmode mode = ios_base::in|ios_base::out which is a non-appending mode.

    You’re interested in the output mode here (ie: ios_base::openmode mode = ios_base::out)

    std::basic_stringbuf::str(const std::basic_string<CharT, Traits, Allocator>& s) operates in two different ways, depending on the openmode:

    1. mode & ios_base::ate == false: (ie: non-appending output streams):

      str will set pptr() == pbase(), so that subsequent output will overwrite the characters copied from s

    2. mode & ios_base::ate == true: (ie: appending output streams):

      str will set pptr() == pbase() + s.size(), so that subsequent output will be appended to the last character copied from s

    (Note that this appending mode is new since c++11)

    More details can be found here.

    If you want the appending behaviour, create your stringstream with ios_base::ate:

    std::stringstream ss(std::ios_base::out | std::ios_base::ate)
    

    Simple example app here:

    #include <iostream>
    #include <sstream>
    
    void non_appending()
    {
        std::stringstream ss;
        std::string s = "hello world";
    
        ss.str(s);
        std::cout << ss.str() << std::endl;
    
        ss << "how are you?";
        std::cout << ss.str() << std::endl;
    }
    
    void appending()
    {
        std::stringstream ss(std::ios_base::out | std::ios_base::ate);
        std::string s = "hello world";
    
        ss.str(s);
        std::cout << ss.str() << std::endl;
    
        ss << "how are you?";
        std::cout << ss.str() << std::endl;
    }
    
    int main()
    {
        non_appending();
        appending();
    
        exit(0);
    }
    

    This will output in the 2 different ways as explained above:

    hello world
    how are you?
    hello world
    hello worldhow are you?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If we consider a std::string implementation that uses reference counting, consider this scenario: int
Consider the following code: std::string my_error_string = Some error message; // ... throw std::runtime_error(std::string(Error:
I have some gaps in the understanding of string::assign method. Consider the following code:
consider this translation unit: #include <map> #include <string> int main() { std::map<std::string, std::size_t> mp;
Consider the following class: class A { public: std::string field_a; std::string field_b; } Now
Consider this example: #include <algorithm> #include <iostream> int main() { std::string str = abcde4fghijk4l5mnopqrs6t8uvwxyz;
The Problem Consider the following vectors: std::vector<std::string> extensions; extensions.push_back(.cpp); extensions.push_back(.CPP); extensions.push_back(.h); extensions.push_back(.H); std::vector<std::string> caselessUniqueExtensions;
Consider this: std::vector<int*> v(1, 0); This compiles fine with VC++10 (no warnings even at
Consider this line: std::wcout << Hello World!; Is it OK to pass char* or
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;

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.