C++json插入数组
C++ json insert into array
我使用的是nlohmann json。我想插入一个数组。我知道在javascript中有一个Array.prototype.splice
,它允许您插入到数组中。在nlohmann的json中有类似的方法吗。
我希望这种情况发生:
//from this:
[1, 2, 3, 5]
//insert at position 3 the value 4
[1, 2, 3, 4, 5]
基本上,我想要类似于std::vector
插入方法的东西。
下面的例子应该有效,假设您使用的是单个includejson.hpp
,并且它位于编译器使用的include目录集中。否则,根据需要修改#include
#include "json.hpp"
#include <iostream>
int main() {
nlohmann::json json = nlohmann::json::array({0, 1, 2});
std::cout << json.dump(2) << "nn";
json.insert(json.begin() + 1, "foo");
std::cout << json.dump(2) << 'n';
}
这应该打印:
[
0,
1,
2
]
[
0,
"foo",
1,
2
]
相关文章:
- C++json插入数组
- 正在插入动态数组
- 如何在向量数组中插入元素?
- 将值插入到标准 c++ 数组的正确索引(升序)中
- C++ - 将元素按升序插入数组的 SEG 错误
- 将二维数组的所有元素插入到一维数组中
- 插入数组时违反写访问冲突
- 尝试将对象插入空指针数组时出现分段错误
- 类数组插入函数
- 将数组的元素插入映射的时间复杂度是多少?
- 当索引位于末尾和空数组时,向数组插入元素时出现问题
- C++ 使用数组插入到二叉搜索树中
- 将 2D 数组插入 std::vector 时"cannot convert from 'const GLfloat [12]' to '_Objty'"错误消息
- 如何使用 mongo-cxx-driver 的 **insert** 函数将包含静态数据的多维数组插入到数据库中
- 如何将字符数组插入浮点向量
- 如何将数组插入无序集合
- 用c++将数组数组插入mongo中
- visual C++将整数的二维数组插入到另一个整数的二维阵列中
- 优化数组插入C/ c++
- c++如何将数组插入哈希集