现在的位置: 首页 > 综合 > 正文

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

2013年12月05日 ⁄ 综合 ⁄ 共 655字 ⁄ 字号 评论关闭

代码片段如下,

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;
}

【上篇】
【下篇】

抱歉!评论已关闭.