std::字符串和占位符



嗨,我想构造一个类似的字符串

using namespace std::placeholders;
std::string str = "Proc.Status_1.power";
//code here that allows me to replace place holder _1 with a number dynamically

C++中有什么方法可以进行这种字符串操作吗。如果没有,还有其他更容易的选择。

我有几个这样的字符串,我想保存在一个undred_map中,并在运行时获取它们,并插入所需的数字来替换占位符。

谢谢。

占位符不是这样工作的。像_1等占位符当前仅用作std::bind的占位符参数。

如果要格式化字符串,请使用std::format(如果您有权访问它(、std::format所基于的格式库,或者使用std::ostringstream来构造字符串。

所以要么

auto str = std::format("Proc.Status{}.power", some_value);

// Using the format library linked to above
auto str = fmt::format("Proc.Status{}.power", some_value);

auto str = (std::ostringstream() << "Proc.Status" << some_value << ".power").str();