rfl::Object
rfl::Object<...>
behaves similarly to std::map<std::string, ...>
and std::unordered_map<std::string, ...>
.
It will be represented in JSON or other formats as an object:
auto bart = rfl::Object<std::string>();
bart["first_name"] = "Bart";
bart["last_name"] = "Simpson";
bart["town"] = "Springfield";
This is results in the following JSON string:
{"first_name":"Bart","last_name":"Simpson","town":"Springfield"}
It supports almost all of the normal features you would expect from such a container. For instance, you can iterate through it just like any other map:
for (const auto& [k, v]: bart) {
std::cout << k << ": " << v << std::endl;
}
However, unlike these containers, the order of fields is preserved. It is also possible to have duplicate keys:
auto bart = rfl::Object<std::string>();
bart["first_name"] = "Bart";
bart["last_name"] = "Simpson";
bart["town"] = "Springfield";
// Note that you need the .insert(...) method,
// because operator[] would overwrite the
// existing field.
bart.insert("first_name", "Lisa");
This results in the following JSON string:
{"first_name":"Bart","last_name":"Simpson","town":"Springfield","first_name":"Lisa"}
There are three different ways of inserting fields:
operator[...]
creates a new field, if a field of this name doesn't already exist, and then assigns it..insert(...)
inserts a new field at the end, potentially creating duplicate field names. Much like std::map
, it supports several types of inputs:
.insert(std::pair(key, value))
.insert(key, value)
.insert(begin, end)
, where begin
and end
are iterators of a container of key-value-pairs.emplace(...)
is an alias for .insert
that exists primarily for reasons of compatability with standard containers.There are three different ways of accessing fields:
operator[...]
creates a new field, if a field of this name doesn't already exist..at(...)
throws an exception, if field of this name doesn't exist..get(...)
returns an rfl::Result
wrapping the field, or an rfl::Error
if the field doesn't exist.Note that it is most efficient, if you access fields in the order that they were placed.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。