crossbind
GitHub
TUTORIAL · STEP 3 OF 7

Strings and vectors

A std::string is a JavaScript string. A std::vector is a vector object, with two helpers that turn it into an array and back.

split returns a std::vector<std::string>, which arrives as a vector object: size() and get(i) read it, and Module.toArray(vector) copies it into an array.

total takes a std::vector<int>, so it needs a vector object too. Module.toVector('VectorInt', [1, 2, 3, 4]) builds one from an array; the first argument names the vector type.

In an app, initNative() runs the module in a Web Worker by default. Every call returns a promise there, and plain arrays are converted for you. The playground runs the module without a worker, so calls return their results directly and vectors take the helpers.

Try it
Add std::vector<int> range(int from, int to); and print Module.toArray(Module.range(1, 5)).
Runs without a compile until you edit the C++.
Console
Press Run, or Ctrl+Enter, to run main.js.
Type to search every guide page and section.
↑↓ navigate↵ openesc close