How to bridge a JavaScript (ragged) array and an std::vector<std::vector<T>> object?

一个人想着一个人 提交于 2019-12-06 04:11:21

When passing an embound vector from a C++ function, such as

std::vector<std::vector<double>> f_wrapper(...);

to Javascript

var result = Module.f_wrapper(...);

The result object is not a Javascript array that implements length property or array-indexed access, so it can appear "empty" if using these to access its data.

But it does expose get and size functions to access the vector:

console.log('Back from C++', result.size(), result.get(0).get(1));

(The double get is because we're returning a vector of vectors)

For completeness, looking into the prototype of the object returned, it seems to expose the following functions.

  • get
  • push_back
  • resize
  • set
  • size

Slightly inconsistently it exposes get and set functions rather than an equivalent of the C++ at function. I suspect that it's not possible for an exactly equivalent function since at returns a reference which allows it to be used as a setter, which isn't possible in Javascript.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!