Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why iterator::end( ) are non-static members and not similar as string::npos?

While iterating through a std::map or std::vector or any container which has iterator in it, is checked against the variable.end() and not something like container<>::end. For example,

map<int, int> var;
for(map<int, int>::iterator it = var.begin(); it != var.end(); it++)
...                                           ^^^^^^^^^^^^^^^

Can't above highlighted part be something like:

it != map<int,int>::end

which is similar to static member string::npos. What can be the reason behind the design decision for providing .end() on per variable bases and not on the per type of container bases ? (i.e. map<int,int>::end and map<int,double>::end would be different; but for every map<int,int> variable, the ::end will be similar.)

like image 504
iammilind Avatar asked Oct 30 '25 20:10

iammilind


1 Answers

This way, some array-based containers could implement an iterator simply as a pointer to an element. The one-past-the-last-element pointer is different for every array.

like image 68
ibid Avatar answered Nov 02 '25 10:11

ibid