Note that there are some explanatory texts on larger screens.

plurals
  1. POboost::python: Python list to std::vector
    primarykey
    data
    text
    <p>Finally I'm able to use std::vector in python using the [] operator. The trick is to simple provide a container in the boost C++ wrapper which handles the internal vector stuff:</p> <pre><code>#include &lt;boost/python.hpp&gt; #include &lt;vector&gt; class world { std::vector&lt;double&gt; myvec; void add(double n) { this-&gt;myvec.push_back(n); } std::vector&lt;double&gt; show() { return this-&gt;myvec; } }; BOOST_PYTHON_MODULE(hello) { class_&lt;std::vector&lt;double&gt; &gt;("double_vector") .def(vector_indexing_suite&lt;std::vector&lt;double&gt; &gt;()) ; class_&lt;World&gt;("World") .def("show", &amp;World::show) .def("add", &amp;World::add) ; } </code></pre> <p>The other challenge is: Howto translate python lists into std::vectors? I tried to add a c++ class expecting a std::vector as parameter and added the corresponding wrapper code:</p> <pre><code>#include &lt;boost/python.hpp&gt; #include &lt;vector&gt; class world { std::vector&lt;double&gt; myvec; void add(double n) { this-&gt;myvec.push_back(n); } void massadd(std::vector&lt;double&gt; ns) { // Append ns to this-&gt;myvec } std::vector&lt;double&gt; show() { return this-&gt;myvec; } }; BOOST_PYTHON_MODULE(hello) { class_&lt;std::vector&lt;double&gt; &gt;("double_vector") .def(vector_indexing_suite&lt;std::vector&lt;double&gt; &gt;()) ; class_&lt;World&gt;("World") .def("show", &amp;World::show) .def("add", &amp;World::add) .def("massadd", &amp;World::massadd) ; } </code></pre> <p>But if doing so, I end up with the following Boost.Python.ArgumentError:</p> <pre><code>&gt;&gt;&gt; w.massadd([2.0,3.0]) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; Boost.Python.ArgumentError: Python argument types in World.massadd(World, list) did not match C++ signature: massadd(World {lvalue}, std::vector&lt;double, std::allocator&lt;double&gt; &gt;) </code></pre> <p>Can anybody tell me how I can access python lists within my c++ function?</p> <p>Thanks, Daniel</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload