[stringstream] How to insert elements at beginning

Hi,

I have a stringstream and I need to add something to the beginning of it.
Is possible doing it without using another string?

1
2
3
4
5
6
7
8
9
10
11
void foo(){
  stringstream ss1;  
  ss1 << "bla bla...";
  bar(ss1);
}

void bar(stringstream ss){
  // Here I want to put something in front of ss
  // to have as result: 
  // ss = "Something bla bla..."
}


Thanks,
Daniele.
no, as far as I know
You have to cheat:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

ostringstream& insert( ostringstream& oss, const string& s )
  {
  streamsize pos = oss.tellp();
  oss.str( s + oss.str() );
  oss.seekp( pos + s.length() );
  return oss;
  }

int main()
  {
  ostringstream oss;
  oss << "world";
  insert( oss, "Hello " );
  oss << "!";
  cout << oss.str() << endl;
  return 0;
  }

Hope this helps.
Thanks a lot!!

Daniele.
Topic archived. No new replies allowed.