I don't do anything, I will turn the STL's code out of the boring. When I see some code, I suddenly have this idea. In fact, this can only be counted a recreational project (maybe I am an extreme part or may be metamorphosis), if you are now Bored, then I will come to me and become a constant, 嘿嘿 STL said here is actually SGI STL, all STL words are all SGI STL, my STL code is actually DEV-C . It's almost that has been touched. Now it is officially started, please take the knife, on STL
First look distance achieved (directory is% include% / c / bits / stl_iterator_base_funcs.h) template
__glibcpp_function_requires (_inputiteratorConcept <_inputiterator>)
TypeName Iterator_Traits <_inputiterator> :: Difference_Type __n = 0; While (__first! = __last) { __first; __ n;} return__n;}
template
__glibcpp_function_requires (_randomaccessiterator ") Return __last;}
The two overload versions of __distance are in order to support input iterators (not supported random access) and random access iterators, and we are using this DISTANCE TEMPLATE
Return __distance (__ number, __last, __iterator_category);}
Where Distance uses the __distance this internal implementation, now take a look at the place we want to modify, in fact, in the Distance Return statement, __ distance is an overload function, where the third parameter needs to call __iterator_category this template function, Then ITERATOR_CATEGORY (this is just a "concept" name) object, which comes to determine the role of Iterator_category. Now we have the motivation of our sharpening to eliminate the two calls of __iterator_category and iterator_category objects, and more similar things to the function is the bias of the function template, but don't count on this thing, because there is no C . Will. There is no road to the sky, we can use class templates. The code is below, it is relatively simple, and I understand. However, I will remember in front #include template __glibcpp_function_requires (_InputIteratorConcept <_InputIterator>) typename iterator_traits <_InputIterator> :: difference_type __n = 0; while (! __first = __last) { __ first; __ n;} return __n;}}; template __glibcpp_function_requires (_InputIteratorConcept <_InputIterator>) typename iterator_traits <_InputIterator> :: difference_type __n = 0; while (! __first = __last) { __ first; __ n;} return __n;}}; template template __glibcpp_function_requires (_randomaccessITorConcept <_randomaccessITerctor>) RETURN __LAST - __FIRST;}}; Top the internal implementation __distance to get it, now you will get Distance template Return __distance <_inputiterator, item_traits <_inputiterator> :: item_category> :: item ";} Get it, let's take a look at the procedure you write with STL, where is it related to the Distance related part of the program is increasing, hahaha, you feel that I admire you (in fact the efficiency difference is very small) // the end