gpt4 book ai didi

c++ - 为什么不为 Json::Value 使用 operator=?

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:53 24 4
gpt4 key购买 nike

例子:

std::string strJson = R"({"foo": "bar"})";
Json::Value root = strJson; // if it implement operator=(std::string&)

代替

std::string strJson = R"({"foo": "bar"})";

Json::CharReaderBuilder builder;
Json::CharReader* reader = builder.newCharReader();

Json::Value json;
std::string errors;

bool parsingSuccessful = reader->parse(
strJson.c_str(),
strJson.c_str() + strJson.size(),
&json,
&errors
);
delete reader;

我觉得前者比后者方便很多。

为什么 Json::Value 不用 operator=?

最佳答案

您可以使用std::istream& operator>>(std::istream&, Value&):

Json::Value root;
std::stringstream(R"({"foo": "bar"})") >> root;

采用std::string 的构造函数是构建一个包含字符串值的值(因此isString() 将返回true)。

关于c++ - 为什么不为 Json::Value 使用 operator=?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50478240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com