STL Source Analysis (algorithm)

Source: Internet
Author: User

The algorithms in STL are implemented based on iterators.

With the implementation of iterators in containers (overloads of operator*, operator++, etc.), most of the algorithm implementations in the STL are very simple.

Let's take a look at an implementation of the Find algorithm:

1Template <classInputiterator,classT>2Inputiterator Find (inputiterator First, Inputiterator last,Constt&value) {3     //Direct use of operator++, operator*, operator!= implementation in iterator4     //default to operator!= with class T5      while(First! = Last && *first! = value) + +First ;6         returnFirst ;7 }8 9Template <classInputiterator,classPredicate>Ten inputiterator find_if (inputiterator First, Inputiterator last, One predicate pred) { A     //can accept a copy function to specify the condition of find -      while(First! = Last &&!pred (*first)) + +First ; -         returnFirst ; the}
View Code

Other basic algorithm implementations are similar, they generally only perform simple data movement, linear lookup, counting, loop traversal and other operations.

More complex algorithms are generally about arranging, sorting and so on, this time just say the SGI STL in the implementation of sort ().

The sort () in SGI STL

The sequencing algorithm in SGI STL mixes the quick sort, heap sort, and insert sort three sorting algorithms.

The overall use of the quick sort, the heap sort is used when splitting to a certain depth (2^k < = n),

Insert sort is the last execution when the split area element size is less than the threshold (16).

Here is the basic framework for sort ():

1Template <classRandomaccessiterator>2Inlinevoidsort (randomaccessiterator first, Randomaccessiterator last) {3     if(First! =Last ) {4         //Use the quick sort, heap sort sort until all the split area element sizes are less than the threshold __stl_threshold =5__introsort_loop (First, Last, Value_type (first), __LG (last-first) *2);6        //Finally, the insert sort is executed once for its split area7 __final_insertion_sort (First, last);8     }9 }Ten  One //Threshold K satisfies 2^k <= n ATemplate <classSize> - Inline size __lg (size n) { - Size K; the      for(k =0; n >1; N >>=1) ++K; -     returnK; -}
View Code

Then look at the implementation of Introsort_loop:

1Template <classRandomaccessiterator,classTclassSize>2 void__introsort_loop (randomaccessiterator first,3Randomaccessiterator Last, t*,4 Size Depth_limit) {5      while(Last-first > __stl_threshold) {//element is small enough to return the Insert sort processing6         if(Depth_limit = =0) {//segmentation deterioration using Heap_sort7Partial_sort (First, last, last);//implemented as Heap_sort8             return;9         }Ten         //Depth One--Depth_limit; A         //key is the middle value of first Middle last -Randomaccessiterator cut =__unguarded_partition -(First, last, T (__median (*first, * (First + (Last-first)/2), the* (Last-1)))); -         //recursive processing [Cut, last] - __introsort_loop (Cut, Last, Value_type (first), depth_limit); -         //return while continue processing [first, Cut] +Last =cut; -     } +}
View Code

STL Source Analysis (algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.