1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢(xún)
      選擇下列產(chǎn)品馬上在線(xiàn)溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問(wèn)題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
      c++調(diào)用python時(shí)參數(shù)傳遞和返回值解析-創(chuàng)新互聯(lián)

      通過(guò)python接口在cpp中調(diào)用python函數(shù)時(shí),構(gòu)建傳參和解析返回值都會(huì)用到python內(nèi)的變量類(lèi)型和c++的變量類(lèi)型之間的轉(zhuǎn)換。
      返回值是numpy等復(fù)雜結(jié)構(gòu)的數(shù)據(jù)時(shí),可以通過(guò)先轉(zhuǎn)換為list等類(lèi)型再返回,或者單獨(dú)構(gòu)建一個(gè)函數(shù),獲取numpy內(nèi)某個(gè)位置的變量的函數(shù)。

      創(chuàng)新互聯(lián)公司專(zhuān)注骨干網(wǎng)絡(luò)服務(wù)器租用十載,服務(wù)更有保障!服務(wù)器租用,雅安服務(wù)器托管 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪(fǎng)問(wèn)。靈活、實(shí)現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專(zhuān)屬高性能服務(wù)器。

      下面幾個(gè)基礎(chǔ)類(lèi)型的返回值解析:

      #python
      #返回一個(gè)參數(shù)
      def returnInt(a,b):
          return a+b
      #返回兩個(gè)相同類(lèi)型的參數(shù)
      def returnTwoInt():
          return 1,2
      #返回字符串
      def returnString():
          srt = 'hello world'
          return srt
      #返回list
      def returnList():
      	para = [1, 2, 3, 4]
          return 1,para.tolist()
      #返回兩個(gè)不同類(lèi)型的參數(shù)
      def returnTwo():
      	para = [1, 2, 3, 4]
          return 1,para.tolist()
      #返回矩陣
      def returnMat():
          img = cv2.imread('test1.bmp',cv2.IMREAD_GRAYSCALE)
          return img
      //調(diào)用python函數(shù),返回的數(shù)據(jù)統(tǒng)一為PyObject類(lèi)型,需要根據(jù)實(shí)際類(lèi)型進(jìn)行解析
      	PyObject* pReturn = PyObject_CallObject(pFunc, pArgs);
      	//當(dāng)不需要傳參時(shí),可以將參數(shù)位置為空
      	PyObject* pReturn = PyObject_CallObject(pFunc,NULL);

      PyObject為結(jié)構(gòu)體,參數(shù)ob_type->tp_name 為數(shù)據(jù)的類(lèi)型
      在這里插入圖片描述

      //不同類(lèi)型參數(shù)傳遞
      	PyObject* p = PyTuple_New(4);
      	for (int i = 0; i< 4; i++)
      		PyTuple_SetItem(p, i, Py_BuildValue("i", i));
      	PyObject* pArgs = PyTuple_New(2);
      	PyTuple_SetItem(pArgs, 0,Py_BuildValue ("i",1));
      	PyTuple_SetItem(pArgs, 0,Py_BuildValue ("O",p));
      	
      //返回為list
      	double temp;
      	int i_size = PyList_Size(pReturn); 
      	for (int i = 0; i< i_size; ++i)
      	{PyArg_Parse(PyList_GetItem(pReturn, i), "d", &temp);
      		std::cout<< "return result is "<< (temp)<< std::endl;
      	}
      //返回為 int 和 list 兩個(gè)不同類(lèi)型的參數(shù)
      	PyArg_Parse(PyTuple_GetItem(pReturn, 0), "d", &temp);
      	std::cout<< "temp is "<< (temp)<< std::endl;
      	PyObject* pReturnlist;
      	PyArg_Parse(PyTuple_GetItem(pReturn, 1), "O", &pReturnlist);
      	i_size = PyList_Size(pReturnli); 
      	for (int i = 0; i< i_size; ++i)
      	{PyArg_Parse(PyList_GetItem(pReturnli, i), "d", &temp);
      		std::cout<< "return result is "<< (temp)<< std::endl;
      	}
      //返回單獨(dú)一個(gè)變量
      	//構(gòu)建輸入
      	PyObject* pArgs = PyTuple_New(2);
      	PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 1));
      	PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", 1));
      	PyObject* pReturn2 = PyObject_CallObject(pFunc, pArgs);
      	//解析
      	int nResult1;	//解析為int
      	PyArg_Parse(pReturn2, "i", &nResult1);
      	std::cout<< "add="<< nResult1<< std::endl;
      	double nResultd;	//解析為double
      	PyArg_Parse(pReturn2, "d", &nResultd);
      	std::cout<< "add="<< nResultd<< std::endl;
      //返回字符串
      	//解析方法1:
      	PyObject* repr = PyObject_Repr(pReturn);
      	PyObject* str = PyUnicode_AsEncodedString(repr, "utf-8", "strict");
      	char* result = PyBytes_AsString(str);
      	std::cout<< result<< std::endl;
      	//解析方法2:
      	char* pstr = NULL;
      	PyArg_Parse(pReturn, "s", &pstr);
      	std::cout<< pstr<< std::endl;
      
      //返回多個(gè)變量
      	//循環(huán)解析
      	int nResult;
      	i_size = PyTuple_Size(pReturn); 
      	for (int i = 0; i< i_size; ++i)
      	{PyArg_Parse(PyTuple_GetItem(pReturn, i), "i", &nResult);
      		std::cout<< "return result is "<< (nResult)<< std::endl;
      	}
      	//直接解析
      	int nResult1;
      	int nResult2;
      	PyArg_ParseTuple(pReturn,"i|i", &nResult1, &nResult2);   
      	std::cout<< "return  "<< nResult5<< ' '<< nResult6<< std::endl;
      //解析二維矩陣
      	//獲得序列長(zhǎng)度,即行數(shù)
      	Py_ssize_t rsize = PyObject_Size(pReturn);
      	std::cout<< rsize<< std::endl;
      	//轉(zhuǎn)換到迭代器,用于提取數(shù)據(jù)
      	PyObject* iter = PyObject_GetIter(pReturn5);
      	while (true)
      	{//獲取矩陣的第一行
      		PyObject* next = PyIter_Next(iter);
      		if (!next) {// nothing left in the iterator
      			break;
      		}
      		if (!PyList_Check(next))
      		{// error, we were expecting a list value }
      		//獲得當(dāng)前行的數(shù)據(jù)數(shù)量
      		Py_ssize_t foosize = PyObject_Size(next);
      		std::cout<< foosize<< std::endl;
      		//轉(zhuǎn)換到迭代器,用于獲取數(shù)據(jù)
      		PyObject* iter2 = PyObject_GetIter(next);
      		while (true)
      		{	PyObject* next2 = PyIter_Next(iter2);
      			if (!next2)
      				break;
      			if (!PyFloat_Check(next2))
      			 {// error, we were expecting a floating point value}
      			double foo = PyFloat_AsDouble(next2);
      			std::cout<< foo<< " ";
      		}
      		std::cout<< std::endl;
      	}

      python手冊(cè),接口的使用示例等
      https://docs.python.org/3/

      //解析常用字段:
      	// b<=>char 0-255的那個(gè)char
      	// c<=>char 單個(gè)字符
      	// h<=>short int
      	// l<=>long int
      	// f<=>float
      	// d<=>double
      	// s<=>char*
      	// O<=>PyObject

      你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧


      當(dāng)前名稱(chēng):c++調(diào)用python時(shí)參數(shù)傳遞和返回值解析-創(chuàng)新互聯(lián)
      當(dāng)前鏈接:http://www.ef60e0e.cn/article/gehhd.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        博客| 三门县| 个旧市| 河西区| 河东区| 广水市| 鹤庆县| 凉山| 胶南市| 定日县| 临沧市| 仁化县| 绍兴县| 东海县| 隆德县| 芒康县| 乌兰察布市| 神农架林区| 苏州市| 贵州省| 邵阳县| 五寨县| 兰州市| 溧水县| 漠河县| 金湖县| 黔西| 江北区| 汉沽区| 兴化市| 湘乡市| 晴隆县| 阿城市| 桂东县| 高台县| 蓬安县| 景洪市| 库伦旗| 高唐县| 衡东县| 安宁市|