Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;json插入到数组中_C++_Json_Nlohmann Json - Fatal编程技术网

C++ C++;json插入到数组中

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,并且它位于编译

我使用的是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插入方法的东西。

假设您使用的是单个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
]