C++ C++;json插入到数组中
我使用的是nlohmann json。我想插入一个数组。我知道在javascript中有一个C++ C++;json插入到数组中,c++,json,nlohmann-json,C++,Json,Nlohmann Json,我使用的是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,并且它位于编译
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插入方法的东西。假设您使用的是单个include
json.hpp,并且它位于编译器使用的include目录集中,那么下面的示例应该可以工作。否则,根据需要修改#include
:
#include "json.hpp"
#include <iostream>
int main() {
nlohmann::json json = nlohmann::json::array({0, 1, 2});
std::cout << json.dump(2) << "\n\n";
json.insert(json.begin() + 1, "foo");
std::cout << json.dump(2) << '\n';
}
欢迎来到堆栈溢出。你能在问题a中说明你已经尝试过的(作为代码)、错误、输入和预期输出吗?谢谢
[
0,
1,
2
]
[
0,
"foo",
1,
2
]