python-list to c++-vector
2011-09-24 19:09
1256 查看
Mmmm ... if you really want to only have list, you may use
boost::python::list as argument : simpler, clearer and boost.python does
the verification job for you !
But if you want to be able to extract every sequence, I would suggest
the use of python iterator as it's more general. For that, you should
use the PyObject_GetIter function (or there is a boost object ???) and
then use the "next" attribute like this :
The only pb I have with my function is you cannot make the difference
between the various exceptions.
Le lun 29/09/2003 à 18:04, Lars Kunert a écrit :
> Hi!
>
> Is this here really the easiest and the fastest way to transfer a small
> (<10000) python-list of integers into a std::vector?!
>
>
> // Using
> =======================================================================
> using namespace boost::python;
>
> // Module
> ======================================================================
> BOOST_PYTHON_MODULE(Iterator_module)
> {
> class_< Ints >("Ints", init<>())
> .def("set", &Ints::set )
> ;
> };
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
--
Pierre Barbier de Reuille
INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France
tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68
boost::python::list as argument : simpler, clearer and boost.python does
the verification job for you !
But if you want to be able to extract every sequence, I would suggest
the use of python iterator as it's more general. For that, you should
use the PyObject_GetIter function (or there is a boost object ???) and
then use the "next" attribute like this :
void set( boost::python::object o ) { try { object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) ) ); while( 1 ) { object obj = extract<object>( iter_obj.attr( "next" )() ); // Should always work int val = extract<int>( obj ); // Should launch an exception if you wannot extract an int ... _ints.push_back(val); } } catch( error_already_set ) { PyErr_Clear(); // If there is an exception (no iterator, extract failed or end of the list reached), clear it and exit the function return; } }
The only pb I have with my function is you cannot make the difference
between the various exceptions.
Le lun 29/09/2003 à 18:04, Lars Kunert a écrit :
> Hi!
>
> Is this here really the easiest and the fastest way to transfer a small
> (<10000) python-list of integers into a std::vector?!
>
>
> // Using
> =======================================================================
> using namespace boost::python;
>
> // Module
> ======================================================================
> BOOST_PYTHON_MODULE(Iterator_module)
> {
> class_< Ints >("Ints", init<>())
> .def("set", &Ints::set )
> ;
> };
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
--
Pierre Barbier de Reuille
INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France
tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68
相关文章推荐
- c++ list, vector, map, set 区别与用法比较(2)
- C++面试题:list和vector有什么区别?
- LeetCode刷题(C++)——Convert Sorted List to Binary Search Tree(Normal)
- c++ list, vector, map, set 区别与用法比较
- 温习C/C++笔记(3)——Vector和List的区别
- C++面试题:list和vector有什么区别?
- Convert Sorted List to Binary Search Tree ------C++ 递归创建平衡二叉查找树
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- C++ STL容器的学习使用(vector、queue、list、set、map)
- LeetCode 12 — Integer to Roman(C++ Java Python)
- 【C++】vector、list、map、set
- LeetCode 141 — Linked List Cycle(C++ Java Python)
- linux C++ vector to array
- c++中list和vector的比较
- How to pass an array from C++ to an embedded python
- leetcode_[python/C++]_121/122/123/188.Best Time to Buy and Sell Stock I/II/III/IV
- C++ list, vector, map, set 区别与用法比较
- C++ 标准库 vector list map使用方法
- 实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
- How to create Linked list using C/C++