Создать строку с параметрами c ++ | форматирование с ++ 20 - PullRequest
1 голос
/ 14 апреля 2020
const char* email = "email@gmail.com";
const char* password = "123456";
          data.add(R"({
             "name": "{*MY_EMAIL_HERE*}",
             "password": "{*MY_PASSWORD_HERE*}",
             "date": { "day": 20, "month": "Apr" }
          })");

Я ищу быстрый способ сделать это, как вы делаете на C языке с printf("%s", myString);

1 Ответ

3 голосов
/ 14 апреля 2020

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;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...