Wheard Hoho to STL :: Distance

xiaoxiao2021-03-06  46

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 inline typename iterator_traits <_InputIterator> :: difference_type __distance (_InputIterator __first, _InputIterator __last, input_iterator_tag) STL in {/ / Concept Requirements

__glibcpp_function_requires (_inputiteratorConcept <_inputiterator>)

TypeName Iterator_Traits <_inputiterator> :: Difference_Type __n = 0; While (__first! = __last) { __first; __ n;} return__n;}

template inline typename iterator_traits <_RandomAccessIterator> :: difference_type __distance (_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag) {// concept requirements

__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 Inline Typename Iterator_traits <_inputIterator>: : Difference_type distance (_inputiterator __first, _inputiterator __last) {// Concept Requirements - Taken Care of in __distance

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

template struct __distance <_InputIterator, input_iterator_tag> {static typename iterator_traits <_InputIterator> :: difference_type implement (_InputIterator __first, _InputIterator __last) {// concept requirements

__glibcpp_function_requires (_InputIteratorConcept <_InputIterator>) typename iterator_traits <_InputIterator> :: difference_type __n = 0; while (! __first = __last) { __ first; __ n;} return __n;}};

template struct __distance <_InputIterator, forward_iterator_tag> {static typename iterator_traits <_InputIterator> :: difference_type implement (_InputIterator __first, _InputIterator __last) {// concept requirements

__glibcpp_function_requires (_InputIteratorConcept <_InputIterator>) typename iterator_traits <_InputIterator> :: difference_type __n = 0; while (! __first = __last) { __ first; __ n;} return __n;}};

template struct __distance <_InputIterator, bidirectional_iterator_tag> {static typename iterator_traits <_InputIterator> :: difference_type implement (_InputIterator __first, _InputIterator __last) {// concept requirements__glibcpp_function_requires (_InputIteratorConcept <_InputIterator>) typename iterator_traits <_InputIterator> :: difference_type __n = 0; while (__first! = __Last) { __first; __ n;} return __n;}};

template struct __distance <_RandomAccessIterator, random_access_iterator_tag> {static typename iterator_traits <_RandomAccessIterator> :: difference_type implement (_RandomAccessIterator __first, _RandomAccessIterator __last) {// concept requirements

__glibcpp_function_requires (_randomaccessITorConcept <_randomaccessITerctor>)

RETURN __LAST - __FIRST;}};

Top the internal implementation __distance to get it, now you will get Distance

template inline typename iterator_traits <_InputIterator> :: difference_type distance (_InputIterator __first, _InputIterator __last) {// concept requirements - taken care of in __distance

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

转载请注明原文地址:https://www.9cbs.com/read-117376.html

New Post(0)