Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenate stringstream in c++

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.

like image 476
Carlitos Overflow Avatar asked Nov 22 '11 18:11

Carlitos Overflow


1 Answers

Should be:

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

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

like image 97
jli Avatar answered Sep 17 '22 05:09

jli