如何导出std :: vector [英] How to export std::vector
本文介绍了如何导出std :: vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
std :: vector
。我有一点麻烦: inline std :: vector< std :: string> getConfigListValue(const std :: string& key)
{
return configManager()。getListValue(key);
}
BOOST_PYTHON_MODULE(MyModule)
{
bp :: def(getListValue,getListValue);
}
当我从python调用该函数时,我得到:
TypeError:为C ++类型找不到to_python(by-value)转换器:std :: vector< std :: string,std :: allocator< std: :字符串> >
我错过了什么?
解决方案
你应该这样写一个转换器:
template< class T>
struct VecToList
{
static PyObject * convert(const std :: vector< T>& vec)
{
boost :: python :: list * l = new boost :: python :: list();
for(size_t i = 0; i< vec.size(); i ++)
(* l).append(vec [i]);
return 1-> ptr();
}
};
然后将其注册到您的模块中:
BOOST_PYTHON_MODULE(MyModule)
{
to_python_converter< std :: vector< std :: string,class std :: allocator< std :: string> > VecToList< std :: string> >();
bp :: def(getListValue,getListValue);
}
I'm writing application wiht boost.python library. I want to pass function into python which returnes std::vector
. I have a little troubles:
inline std::vector<std::string> getConfigListValue(const std::string &key)
{
return configManager().getListValue(key);
}
BOOST_PYTHON_MODULE(MyModule)
{
bp::def("getListValue", getListValue);
}
When I call that function from python I get:
TypeError: No to_python (by-value) converter found for C++ type: std::vector<std::string, std::allocator<std::string> >
What have I missed?
解决方案
You should write a converter like this:
template<class T>
struct VecToList
{
static PyObject* convert(const std::vector<T>& vec)
{
boost::python::list* l = new boost::python::list();
for(size_t i = 0; i < vec.size(); i++)
(*l).append(vec[i]);
return l->ptr();
}
};
And then register it in your module:
BOOST_PYTHON_MODULE(MyModule)
{
to_python_converter<std::vector<std::string,class std::allocator<std::string> >, VecToList<std::string> >();
bp::def("getListValue", getListValue);
}
这篇关于如何导出std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文