I'm a Objective-C dev and sometimes I have to deal with C/C++ code. I have a function written in C++, it logs an event with name, for example, Level_12_Pack_10. I want to create a dynamic C++ string like that, then I can change level and pack numbers. In Objective C, it's easy with some lines of code: [NSString stringwithformat] but in C++, it's a bit difficult for me. Could anyone help me do it?
2 Answers
I don't think C++ supports strings with built-in changeable variables like that. It would be too over-the-top to make a class to format the string like that. Probably the closest thing you can get is to use stringstreams:
#include <sstream>
string makeMyString(int level, int pack) {
stringstream ss;
ss << "Level_" << level << "_Pack_" << pack;
return ss.str();
}
If you have a string that you need to read and change the values inside, a similar function could be used.