Often it is not the bonds around the atom that you wish to loop over but
the neighboring atoms. One way to do this would be to use a
OEBondBase iterator, and then use the OEBondBase method GetNbr which
takes an OEAtomBase* as input, and returns the OEAtomBase* of the atom
across the bond from the input atom.
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 void ShowNeighbors(OEAtomBase *atm)
10 {
11 OEIter<OEBondBase> bond;
12 OEAtomBase *nbor;
13
14 cerr << "Neighbors: ";
15 for (bond = atm->GetBonds(); bond; ++bond)
16 {
17 nbor = bond->GetNbr(atm);
18 cerr << nbor->GetIdx() << " ";
19 }
20 cerr << endl;
21 }
22
23 int main()
24 {
25 OEMol mol;
26 OEParseSmiles(mol, "c1ccccc1");
27
28 OEIter<OEAtomBase> atom;
29 for (atom=mol.GetAtoms();atom;++atom)
30 {
31 cerr << "Atom: " << atom->GetIdx() << " ";
32 ShowNeighbors(atom);
33 }
34 return 0;
35 }
However, this can be done even more conveniently using the OEAtomBase GetAtoms method, which returns the iterator of neighboring atoms to an
atom.
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 void ShowNeighbors(OEAtomBase *atm)
10 {
11 OEIter<OEAtomBase> nbor;
12
13 cerr << "Neighbors: ";
14 for (nbor = atm->GetAtoms(); nbor; ++nbor)
15 {
16 cerr << nbor->GetIdx() << " ";
17 }
18 cerr << endl;
19 }
20
21 int main()
22 {
23 OEMol mol;
24 OEParseSmiles(mol, "c1ccccc1");
25
26 OEIter<OEAtomBase> atom;
27 for (atom=mol.GetAtoms();atom;++atom)
28 {
29 cerr << "Atom: " << atom->GetIdx() << " ";
30 ShowNeighbors(atom);
31 }
32 return 0;
33 }