The example above shows how to use an OEChem iterator to loop over
objects, but didn't actually use them. OEChem iterators provide four
operators to allow the user to access the object at the current
iterator position. Implicit casting or the -> operator may be used
to get a pointer to the current object, and implicit casting or the *
operator can be used to get a reference to a given object.
For example, if variable iter has type OEIter<T> , then (T*)iter is a
pointer to the current item, *iter and (T&)iter are of type
T&. These operators mean that in most cases an OEChem iterator
OEIter<T> behaves identically to a T*.
For example, to list the atomic numbers of atoms in a molecule:
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 ListAtomicNumbers(OEMolBase &mol)
10 {
11 OEIter<OEAtomBase> atom;
12
13 for (atom=mol.GetAtoms(); atom; ++atom)
14 cout << atom->GetAtomicNum() << endl;
15 }
16
17 int main()
18 {
19 OEMol mol;
20 OEParseSmiles(mol, "c1ccccc1");
21 ListAtomicNumbers(mol);
22 return 0;
23 }
This routine could also be written with an explicit assignment to an
OEAtomBase* .
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 ListAtomicNumbers(OEMolBase &mol)
10 {
11 OEIter<OEAtomBase> atom;
12 OEAtomBase *aptr;
13
14 for (atom=mol.GetAtoms(); atom; ++atom)
15 {
16 aptr = atom;
17 cout << aptr->GetAtomicNum() << endl;
18 }
19 }
20
21 int main()
22 {
23 OEMol mol;
24 OEParseSmiles(mol, "c1ccccc1");
25 ListAtomicNumbers(mol);
26 return 0;
27 }
Comparing these two examples shows how iterators and pointers behave
similarly. The OEAtomBase method GetAtomicNum , which returns the
atomic number of the given atom, will be described later.
The implicit casts of OEIter<T> to either a T& or
T* are most useful when passing the object to a function which
takes T by reference or by pointer.
1 #include "openeye.h"
2 #include <iostream>
3 #include "oeplatform.h"
4 #include "oesystem.h"
5 #include "oechem.h"
6
7 using namespace OESystem;
8 using namespace OEChem;
9 using namespace std;
10
11 void PrintAtomicNumber(const OEAtomBase *atom)
12 {
13 cout << atom->GetAtomicNum() << endl;
14 }
15
16 void PrintAromatic(const OEAtomBase &atom)
17 {
18 if(atom.IsAromatic())
19 cout << "Is Aromatic" << endl;
20 else
21 cout << "Isn't Aromatic" << endl;
22 }
23
24 int main()
25 {
26 OEMol mol;
27 OEParseSmiles(mol, "c1ccccc1");
28
29 OEIter<OEAtomBase> atom;
30 for(atom = mol.GetAtoms();atom;++atom)
31 {
32 PrintAtomicNumber(atom);
33 PrintAromatic(atom);
34 }
35
36 return 0;
37 }