I usually do it something like the following:
class MyClass {
public:
const unsigned int GetNumberOfItems() { return v.size(); }
T* GetItemNumber(const unsigned int n)
{
// 3 options here, thrown your own exception type, or use the std one, or
// or just return NULL meaning nothing there or out of range.
try{
return v.at(n);
} catch (std::out_of_range &e){
}
return NULL;
}
vector<T> v;
};
Then you can just do something like:
MyClass cl;
int count = cl.GetNumberOfItems();
for (int i = 0; i < cl.GetNumberOfItems(); i++){
T* item = cl.GetItemNumber(i);
}
No iterators to the outside world required. If you have ever have to expose something like this to a standard C API then it's very easy to expose.