empty()
is not “an iterator member function”. You are not calling empty()
on the iterator – you are dereferencing the iterator, and calling empty()
on the vector element that the iterator refers to. It might be clearer if you replace iter->empty()
with an equivalent form (*iter).empty()
In still other words,
auto iter = vec.begin();
iter->empty();
is equivalent to
vec[0].empty();
Here, iterators are not involved at all; hopefully, this would make the point extra clear.
Now, it so happens that std::string
does have a member function empty()
, while int
of course does not (seeing as it’s not a class type and so can’t have any member functions). That’s why your code compiles with vector<string>
but not vector<int>
.
solved Does using the iterator member function “empty()” only work on certain vector types?