concatenate stringstream in c++

Carlitos Overflow picture Carlitos Overflow · Nov 22, 2011 · Viewed 33.6k times · Source

How can I concatenate two stringstreams?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "types.h"    

int main () {
    char dest[1020] = "you";
    char source[7] = "baby";
    stringstream a,b;
    a << source;
    b << dest;
    a << b; /*HERE NEED CONCATENATE*/
    cout << a << endl;
    cout << a.str() << endl;
    return 0;
}

The output is the following in both tries:

0xbf8cfd20
baby0xbf8cfddc

The desired output is babyyou.

Answer

jli picture jli · Nov 22, 2011

Should be:

b << dest;
a << b.str();

stringstream::str returns the underlying string in the stringstream.