Don't iterate over the cities in that way, you may not get all. Use the same code like for the units.
But please don't do that at all
. The CyPlayer class has the command int getBuildingClassCount (BuildingClassTypes eIndex) .
That will be much faster.
But please don't do that at all

That will be much faster.