Skip to content

Instantly share code, notes, and snippets.

@marcinwol
Created November 5, 2015 04:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save marcinwol/b8df949bf8009cf856a3 to your computer and use it in GitHub Desktop.
Save marcinwol/b8df949bf8009cf856a3 to your computer and use it in GitHub Desktop.
Boost python vector to py list and py list to vector
#include <iostream>
#include <vector>
#include <memory>
#include "boost/shared_ptr.hpp"
#include "boost/python.hpp"
#include "boost/python/stl_iterator.hpp"
using namespace std;
template<typename T>
inline
std::vector< T > py_list_to_std_vector( const boost::python::object& iterable )
{
return std::vector< T >( boost::python::stl_input_iterator< T >( iterable ),
boost::python::stl_input_iterator< T >( ) );
}
template <class T>
inline
boost::python::list std_vector_to_py_list(std::vector<T> vector) {
typename std::vector<T>::iterator iter;
boost::python::list list;
for (iter = vector.begin(); iter != vector.end(); ++iter) {
list.append(*iter);
}
return list;
}
@okmechak
Copy link

okmechak commented Jun 3, 2020

What next to do with list? How to past it to python? For classes we use syntax class_..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment