3.2 Reading Molecules with Iterators

An additional way of reading molecules in OEChem is through the use of oemolstream iterators. OEChem iterators syntactically resemble the for loops common in most programming languages.

#include "oechem.h"

using namespace OEChem;
using namespace OESystem;

int main()
{
  OEIter<OEMolBase> mol;
  oemolistream ims;
  oemolostream oms;

  for (mol=ims.GetMolBases(); mol; ++mol)
    oms << mol;
  return 0;
}

An OEChem iterator of type OEIter<T>, where T is a type, such as int, float, or OEMol, behaves like a pointer to T once it has been assigned. In the example above, mol behaves like a OEMolBase* during the loop. Iterators have functions in addition to those of the iterated type. These iterator functions include (1) that (bool)mol tests whether the iterator has reached the end (for oemolstream iterators this means the end of the file), and (2) that ++mol advances the iterator to the next position.