Solution_1:
Современный C ++ делает это очень простым.
C ++ 20
Общий случай
C ++ 20 вводит формат std ::, который позволяет вам делать именно это. Он использует поля замены, аналогичные полям python:
#include <iostream>
#include <format>
int main() {
std::cout << std::format("Hello {}!\n", "world");
}
Ознакомьтесь с полной документацией! Это огромное улучшение качества жизни.
Solution_2:
в этом случае
с использованием nlohmann / json (https://github.com/nlohmann/json)
#include <nlohmann/json.hpp>
// for convenience
using json = nlohmann::json;
// create an empty structure (null)
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
// add a Boolean that is stored as bool
j["happy"] = true;
// add a string that is stored as std::string
j["name"] = "Niels";
// add another null object by passing nullptr
j["nothing"] = nullptr;