formatting strings without sprintf

Jul 10, 2011 at 9:03pm
Hi

is there any native class in C++ that provides same capability that sprintf for strings ?
I mean, formatting directly to a string instead of :
char test[32]
char *first="test";
string a;
sprintf(test,"This is a %s\n",first);
a=test;


(I'm in Linux GNU C++ now)
Last edited on Jul 10, 2011 at 9:04pm
Jul 10, 2011 at 9:48pm
stringstreams

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> // cout
#include <string> // string
#include <sstream> // stringstream

using namespace std;

string get_a_string()
{
  int foobar = 10;

  stringstream ss;
  ss << "This is a test\n";
  ss << "I have " << foobar << " foobars.";

  return ss.str();
}

int main()
{
  string foo = get_a_string();
  cout << foo;
}
Jul 10, 2011 at 10:43pm
Thanks!
Topic archived. No new replies allowed.