6.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, not including bonds to implicit hydrogen  atoms.

 1 #include "openeye.h"
 2 #include "oechem.h"
 3 #include <iostream>
 4
 5 using namespace OESystem;
 6 using namespace OEChem;
 7 using namespace std;
 8
 9 unsigned int MyGetExplicitDegree(OEAtomBase *atm)
10 {
11   OEIter<OEBondBase> bond;
12   unsigned int result = 0;
13
14   for (bond = atm->GetBonds(); bond; ++bond)
15     ++result;
16   return result;
17 }
18
19
20 int main()
21 {
22   int degree;
23   OEMol mol;
24   OEParseSmiles(mol, "c1ccccc1");
25
26   OEIter<OEAtomBase> atom;
27   for (atom=mol.GetAtoms();atom;++atom)
28   {
29     degree = MyGetExplicitDegree(atom);
30     cout << "Atom " << atom->GetIdx() << " has degree " << degree << endl;
31   }
32   return 0;
33 }

Listing:6.5 Looping over the bonds of an atom