Collection我有一个管理 a std::vector<Element>(该类的私有成员)的C++ 类。
在 C++ 中,我可以使用begin()和end()迭代器(它们只是vector's 迭代器的类型定义)来迭代向量,如下所示:
Collection col;
for (Collection::const_iterator itr = col.begin(); itr != col.end(); itr++)
{
std::cout << itr->get() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
现在我希望用 Python 做类似的事情,例如:
import example
el = example.Element()
el.set(5)
col = example.Collection()
col.add(el)
for e in col:
print e.get()
Run Code Online (Sandbox Code Playgroud)
但这会导致:
类型错误:“集合”对象不可迭代
我无法以生成__iter__Python 类(我认为这是它唯一需要的)的方式配置 SWIG Collection。我该怎么做?
这是我的代码:
示例.h:
#include <vector>
class Element
{
public:
Element();
~Element();
int get() const;
void set(const int var);
private: …Run Code Online (Sandbox Code Playgroud)