我已经阅读了几个地方,c_str()和data()(在STL和其他实现中)之间的区别c_str()是总是空终止而data()不是.据我在实际实现中看到的,他们要么做同样的事情,要么做data()电话c_str().
我在这里错过了什么?在哪种情况下使用哪一个更正确?
我想在我的宠物项目中嵌入一些python.我已将问题减少到以下代码:
#include <Python.h>
#include "iostream"
int main(int argc, char *argv[])
{
Py_Initialize();
PyObject *globals = Py_BuildValue("{}");
PyObject *locals = Py_BuildValue("{}");
PyObject *string_result = PyRun_StringFlags(
"a=5\n"
"s='hello'\n"
"d=dict()\n"
,
Py_file_input, globals, locals, NULL);
if ( PyErr_Occurred() ) {PyErr_Print();PyErr_Clear();return 1;}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(我知道我没有清理任何参考文献.这是一个例子.)
它可以编译
c++ $(python-config --includes) $(python-config --libs) test.cpp -o test
Run Code Online (Sandbox Code Playgroud)
如果我运行它,我会收到以下错误:
$ ./test
Traceback (most recent call last):
File "<string>", line 3, in <module>
NameError: name 'dict' is not defined
Run Code Online (Sandbox Code Playgroud)
看来内置函数没有加载.我也import什么都没有.我明白了__import__.如何加载丢失的模块或我遗漏的任何内容?
谢谢.