Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ string substitution with the string format operator [duplicate]

Tags:

c++

I'm somewhat new to C++, coming from python. One piece of functionality I really miss is the string format operator. I've seen plenty of examples where this can be used in the printf() function, however, sometimes it is handy just to substitute placeholders in a string variable. Here is an example from python using the mysqldb module:

...
stmt = 'INSERT INTO %s(pid, starttime) VALUES("%s","%s")' % ('pids', int(p0.pid), episode[0][1])
cursor.execute(stmt)

Can you do something similar in C++. I'm not finding any examples googling.

like image 565
nomadicME Avatar asked Oct 27 '25 19:10

nomadicME


1 Answers

Check out the Boost format library.

It can do something like

str(format("writing %s,  x=%s : %d-th step \n") % "toto" % 40.23 % 50)
like image 127
Ken Bloom Avatar answered Oct 29 '25 10:10

Ken Bloom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!