5.4 Looping over the Bonds of an Atom

The exact same idiom is used for iterating over the bonds attached to an atom. The OEAtomBase method GetBonds is used to return an iterator over the bonds connected to that atom. The example below shows how to use this iterator to determine the explicit degree of an atom, i.e. the number of bonds to it, other than any to implicit hydrogen atoms.

#include "oechem.h"
#include <iostream>

using namespace OESystem;
using namespace OEChem;
using namespace std;

unsigned int MyGetExplicitDegree(OEAtomBase *atm)
{
  OEIter<OEBondBase> bond;
  unsigned int result = 0;

  for (bond = atm->GetBonds(); bond; ++bond)
    ++result;
  return result;
}

int main()
{
  int degree;
  OEMol mol;
  OEParseSmiles(mol, "c1ccccc1");

  OEIter<OEAtomBase> atom;
  for (atom=mol.GetAtoms();atom;++atom)
  {
    degree = MyGetExplicitDegree(atom);
    cout << "Atom " << atom->GetIdx() << " has degree " << degree << endl;
  }
  return 0;
}