(ת)ʹÓÃC/C++À©Õ¹Python
ʹÓÃC/C++À©Õ¹Python
·Òë:
gashero
Èç¹ûÄã»áÓÃC£¬ÊµÏÖPythonǶÈëÄ£¿éºÜ¼òµ¥¡£ÀûÓÃÀ©Õ¹Ä£¿é¿É×öºÜ¶àPython²»·½±ã×öµÄÊÂÇ飬ËûÃÇ¿ÉÒÔÖ±½Óµ÷ÓÃC¿âºÍϵͳµ÷Óá£
ΪÁËÖ§³ÖÀ©Õ¹£¬Python API¶¨ÒåÁËһϵÁк¯Êý¡¢ºêºÍ±äÁ¿£¬ÌṩÁ˶ÔPythonÔËÐÐʱϵͳµÄ·ÃÎÊÖ§³Ö¡£PythonµÄC APIÓÉCÔ´Âë×é³É£¬²¢°üº¬ “Python.h” Í·Îļþ¡£
±àдÀ©Õ¹Ä£¿éÓëÄãµÄϵͳÏà¹Ø£¬ÏÂÃæ»áÏê½â¡£
Ŀ¼
1 Ò»¸ö¼òµ¥µÄÀý×Ó
2 ¹ØÓÚ´íÎóºÍÒì³£
3 »Øµ½Àý×Ó
4 Ä£¿é·½·¨±íºÍ³õʼ»¯º¯Êý
5 ±àÒëºÍÁ¬½Ó
6 ÔÚCÖе÷ÓÃPythonº¯Êý
7 ½âÎö´«¸øÀ©Õ¹Ä£¿éº¯ÊýµÄ²ÎÊý
8 ½âÎö´«¸øÀ©Õ¹Ä£¿éº¯ÊýµÄ¹Ø¼ü×Ö²ÎÊý
9 ¹¹ÔìÈÎÒâÖµ
10 ÒýÓüÆÊý
10.1 PythonÖеÄÒýÓüÆÊý
10.2 ÓµÓйæÔò
10.3 ΣÏյı¡±ù
10.4 NULLÖ¸Õë
11 ʹÓÃC++±àдÀ©Õ¹
12 Ìṩ¸øÆäËûÄ£¿éÒÔC API
1 Ò»¸ö¼òµ¥µÄÀý×Ó
ÏÂÃæµÄÀý×Ó´´½¨Ò»¸ö½Ð×ö “spam” µÄÀ©Õ¹Ä£¿é£¬µ÷ÓÃC¿âº¯Êý system() ¡£Õâ¸öº¯ÊýÊäÈëÒ»¸öNULL½áβµÄ×Ö·û´®²¢·µ»ØÕûÊý£¬¿É¹©Pythonµ÷Ó÷½Ê½ÈçÏÂ:
>>> import spam
>>> status=spam.system("ls -l")
Ò»¸öCÀ©Õ¹Ä£¿éµÄÎļþÃû¿ÉÒÔÖ±½ÓÊÇ Ä£¿éÃû.c »òÕßÊÇ Ä£¿éÃûmodule.c ¡£µÚÒ»ÐÐÓ¦¸Ãµ¼ÈëÍ·Îļþ:
#include <Python.h>
Õâ»áµ¼ÈëPython API¡£
Warning
ÒòΪPythonº¬ÓÐһЩԤ´¦Àí¶¨Ò壬ËùÒÔÄã±ØÐëÔÚËùÓзDZê׼ͷÎļþµ¼Èë֮ǰµ¼ÈëPython.h ¡£
Python.hÖÐËùÓÐÓû§¿É¼ûµÄ·ûºÅ¶¼ÓÐ Py »ò PY µÄǰ׺£¬³ý·Ç¶¨ÒåÔÚ±ê׼ͷÎļþÖС£ÎªÁË·½±ã “Python.h” Ò²°üº¬ÁËһЩ³£Óõıê׼ͷÎļþ£¬°üÀ¨<stdio.h>£¬<string.h>£¬<errno.h>£¬<stdlib.h>¡£Èç¹ûÄãµÄϵͳûÓкóÃæµÄÍ·Îļþ£¬Ôò»áÖ±½Ó¶¨Ò庯Êý malloc() ¡¢ free() ºÍ realloc() ¡£
ÏÂÃæÌí¼ÓC´úÂëµ½À©Õ¹Ä£¿é£¬µ±µ÷Óà “spam.system(string)” ʱ»á×ö³öÏìÓ¦:
static PyObject*
spam_system(PyObject* self, PyObject* args) {
const char* command;
int sts;
if (!PyArg_ParseTuple(args,"s",&command))
return NULL;
sts=system(command
Ïà¹ØÎĵµ£º
ACE×ÔÊÊÅäͨÐÅ»·¾³£¨ADAPTIVE Communication Environment£©ÊÇ¿ÉÒÔ×ÔÓÉʹÓᢿª·ÅÔ´ÂëµÄÃæÏò¶ÔÏó£¨OO£©¿ò¼Ü£¨Framework£©£¬ÔÚÆäÖÐʵÏÖÁËÐí¶àÓÃÓÚ²¢·¢Í¨ÐÅÈí¼þµÄºËÐÄģʽ¡£ACEÌṩÁËÒ»×é·á¸»µÄ¿É¸´ÓÃC++ Wrapper Facade£¨°ü×°Íâ¹Û£©ºÍ¿ò¼Ü×é¼þ£¬¿É¿çÔ½¶àÖÖƽ̨Íê³ÉͨÓõÄͨÐÅÈí¼þÈÎÎñ£¬ÆäÖаüÀ¨£ºÊ¼þ¶à··ÖÀëºÍʼþ´¦À ......
//ÊäÈëÒ»¸öÊý×飬ÔÙÐÞ¸ÄÕâ¸öÊý×éËùÓÐÔªËØ£¬ÈçºÎʵÏÖ£¿
int main()
{
vector<int> a;
int i(0);
while(cin>>i)
a.push_back(i);
//////////////////////////////////////////////////////Êä³ö½¨Á¢µÄÊý×飺
cout << "µÃµ½µÄÊý×éΪ£º" << ......
¶þ·Ö²éÕҵĴúÂë.
int bfind(int* a,int len,int val)
{
int m = len/2;
int l = 0;
int r = len;
while(l!=m && r!= m)
{
if(a[m] > val)
& ......
Ö¸ÕëÊÇc/c++ÓïÑÔµÄÌØÉ«£¬¶øÊý×éÃûÓëÖ¸ÕëÓÐÌ«¶àµÄÏàËÆ£¬ÉõÖÁºÜ¶àʱºò£¬Êý×éÃû¿ÉÒÔ×÷ΪָÕëʹÓá£ÓÚÊǺõ£¬ºÜ¶à³ÌÐòÉè¼ÆÕ߾ͱ»¸ãºýÍ¿ÁË¡£¶øÐí¶àµÄ´óѧÀÏʦ£¬ËûÃÇÔÚcÓïÑԵĽÌѧ¹ý³ÌÖÐÒ²´íÎóµÃ¸øѧÉú½²½â£º\"Êý×éÃû¾ÍÊÇÖ¸Õë\"¡£ºÜÐÒÔË£¬ÎҵĴóѧÀÏʦ¾ÍÊÇÆäÖÐÖ®Ò»¡£Ê±ÖÁ½ñÈÕ£¬ÎÒÈÕ¸´Ò»ÈյؽøÐÐ×Åc/c++ÏîÄ¿µÄ¿ª·¢£¬¶øÉí± ......
Data Type Ranges
C/C++ recognizes the types shown in the table below.
Type Name Bytes Other Names Range of Values
&nb ......