1

在 Open Babel 库中,为 OBMol 类定义了许多迭代器对象,例如OBMolAtomiter。在链接页面上,有以下代码示例说明了用法。

  #include <openbabel/obiter.h>
  #include <openbabel/mol.h>

  OpenBabel::OBMol mol;
  double exactMass = 0.0;
  FOR_ATOMS_OF_MOL(a, mol)
  {
     // The variable a behaves like OBAtom* when used with -> and * but
     // but needs to be explicitly converted when appearing as a parameter
     // in a function call - use &*a

     exactMass +=  a->GetExactMass();
  }

FOR_ATOMS_OF_MOL(a, mol)扩展为一个 for 循环,a被声明为迭代器类型。mol是要迭代的现有分子)

我想问,为什么&*p评论中描述的东西是必要的。当我将迭代器传递给需要指针的函数时,代码会编译,但程序的行为很奇怪。

我试着用谷歌搜索它,我找到了关于iterator_traits的页面,它是否相关?

4

1 回答 1

2

类型的FOR_ATOMS_OF_MOL(a, mol)宏构造。要返回s,and运算符已被重载。这就是为什么不能直接传递给函数,而是表现得好像是一个.aOBMolAtomIterOBAtom*->a*aa->aOBAtom *

http://openbabel.org/api/2.2.0/classOpenBabel_1_1OBMolAtomIter.shtml

于 2012-04-25T16:53:34.187 回答