且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C++ python 交互之 python.dict to c++ map

更新时间:2022-09-27 23:14:18

代码片段如下,
typedef map<string ,string> smap;//要转换的目的map
转换函数
string req_update_userprofile_pack_data2(std::string workkey,PyObject *dict_user)
{
Py_ssize_t i, j;
i = 0;
PyObject *key;
PyObject *value;
smap m_profilelist;
while (PyDict_Next(dict_user, &i, &key, &value)) //C++中遍历python的dict对象。此为关键代码
{
char * skey = PyString_AS_STRING(key); 
char * svalue = PyString_AS_STRING(value); 
string sskey(skey);
string ssvalue(svalue);
if (sskey != "")
{
cout<< ssvalue << endl; 
m_profilelist[sskey] = ssvalue;
}
cout << skey << endl;
}
cout <<"get bloadtype"<<m_profilelist["bloodType"] << endl;
.....还有其他项目中的代码
return buf;
}


本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/751861,如需转载请自行联系原作者