2

I am currently learning C++ and I cannot find how to create a string with a formatter to take multiple parameters such as using sprintf but for an unknown string length.

What I want do is something like

string myString = string.format("Hello, here is %s %s and another %s", par1, par2, par3);

I know in C there is the asprintf function where it works out how long the string will be and it malloc's the memory and you need to free it when finished, however, this doesn't seem to be available for C++, although sprintf is. Everything I've seen on google about asprintf seems to mostly focus on Linux, whereas I need cross platform.

Everything I've seen about C++ and string formatting you need to define a char array of fixed length and use sprintf, but I am not going to know the length of the string so I can't do this.

halfer
  • 19,824
  • 17
  • 99
  • 186
Boardy
  • 35,417
  • 104
  • 256
  • 447
  • 1
    Boost has something like this, but standard C++ doesn't. You can however concatenate strings (`std::string`s that is, not the old string literals) with `operator +`. – Baum mit Augen Oct 05 '16 at 11:05
  • 2
    You should've encountered the `<<` operator by now. It is the `<<` that's used to format strings of arbitrary size and parameters. – Sam Varshavchik Oct 05 '16 at 11:07
  • 1
    Use a `std::ostringstream`. – Leon Oct 05 '16 at 11:09
  • Checkout if this answer helps http://stackoverflow.com/questions/12233710/how-do-i-use-the-ostringstream-properly-in-c – Hennio Oct 05 '16 at 11:34
  • @BaummitAugen I've seen a lot of things mention boost, is that something that is recommended for C++ development – Boardy Oct 05 '16 at 11:52
  • @Boardy Always depends on your usecase of course, but it is fairly wide spread. It's open source with a permissive license, highly portable and of high quality. Some components are kind of big though, and not everything is documented as well as one would like. It really does solve a lot of problems in the end. – Baum mit Augen Oct 05 '16 at 11:56
  • Thanks I'll check it out – Boardy Oct 05 '16 at 12:07

3 Answers3

2

In addition to the existing excellent answer, you may consider the Boost Format library.

Example:

std::string myString = str(boost::format("Hello, here is %s %s an another %s") % par1 % par2 % par3);
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
1

Get the book The Standard C++ Library by Josuttis. It will give you the complete string interface and much, much more. You appear to be thinking C, not C++. You could of course use a C interface like sprintf() to load a char[] and then copy that to a C++ string. That is usually a bad idea.

Two ways to do what you ask:
string myString("Hello, here is ");
myString += par1;
myString += " ";
myString += par2;
myString += " and another ";
myString += par3;

stringstream foo;
foo << "Hello, here is " << par1 << " " << par2 << " and another " << par3;
string myString(foo.str());
  • Thanks I do think I am getting mixed up sometimes with C and C++ as they are somewhat similar in places. I went for the stringstream. – Boardy Oct 05 '16 at 11:51
1

There are lots of answers. As C++ strings get very long, you want to use the std::stringstream to build them. This allows you to write to a string as though it were a file in memory, and it is written to handle very large strings efficiently. The C function snprintf() returns the number of characters it would have written if passed a null pointer. So you need to call it twice, once to get the size, then allocate the buffer and call again to format. It's a good solution for strings which are expected to be quite short and with a defined format, but might get arbitrarily long, like a string containing someone's name.

Note that printf() formats are convenient and easy to use for basic output of integers, string, and reals, but they don't scale up to user-defined objects because there's no accepted way of writing a toString() method and destroying the string after the call. They also can't handle arrays of objects. There is an accepted convention that overloading << writes a text representation of an object to a stream.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18