C++代码如下:src.cpp
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
typedef map<string,int> simap;
typedef vector<simap> mvector;
simap A()
{
simap m;
m["aaa"] = 1;
m["bbb"] = 2;
m["ccc"] = 3;
m["ddd"] = 4;
m["eee"] = 5;
m["fff"] = 6;
return m;
}
mvector B(simap s)
{
mvector m;
m.push_back(s);
return m;
}
int C(mvector m)
{
int q = m[0]["aaa"];
return q;
}
转换代码如下:
#include <string>
#include <map>
#include <vector>
using namespace std;
typedef map<string,int> simap;
typedef vector<simap> mvector;
simap A()
{
simap m;
m["aaa"] = 1;
m["bbb"] = 2;
m["ccc"] = 3;
m["ddd"] = 4;
m["eee"] = 5;
m["fff"] = 6;
return m;
}
mvector B(simap s)
{
mvector m;
m.push_back(s);
return m;
}
int C(mvector m)
{
int q = m[0]["aaa"];
return q;
}
转换代码如下:
#src4py.cpp
#include <boost/python.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include "src.cpp"
using namespace boost::python;
BOOST_PYTHON_MODULE(test)
{
class_<simap>("pySImap")
.def( map_indexing_suite<simap>() )
;
class_<mvector>("mvector")
.def( vector_indexing_suite< mvector > () )
;
def("a",A);
def("b",B);
def("c",C);
}
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include "src.cpp"
using namespace boost::python;
BOOST_PYTHON_MODULE(test)
{
class_<simap>("pySImap")
.def( map_indexing_suite<simap>() )
;
class_<mvector>("mvector")
.def( vector_indexing_suite< mvector > () )
;
def("a",A);
def("b",B);
def("c",C);
}
调用代码如下:
#test.py
import test
mapcontainer = test.pySImap()
mapcontainer = test.a()
d = test.b(mapcontainer)
q = test.c(d)
print d[0]['aaa']
print q
mapcontainer = test.pySImap()
mapcontainer = test.a()
d = test.b(mapcontainer)
q = test.c(d)
print d[0]['aaa']
print q
本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/751837,如需转载请自行联系原作者