Week 1Programming Style (C++)Basic components of STLPowerPoint PresentationSlide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Week 11. Programming Style2. Standard Template Library3. Merge-Sort AlgorithmProgramming Style (C++) 1. Names of functions, classes, structures, variables and constants2. Expressions and statements 3. CommentsBasic components of STL1. Containers2. Algorithms 3. IteratorsOnline documentation & linkshttp://www.sgi.com/tech/stl/Merge algorithm:OutputIterator merge(InputIterator1 first1,InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result);Merge combines two sorted ranges [first1, last1) and [first2, last2) into a single sorted range. That is, it copies elements from [first1, last1) and [first2, last2) into [result, result + (last1 - first1) + (last2 - first2)) such that the resulting range is in ascending order. The return value is result + (last1 - first1) + (last2 - first2).Include statements:#include <vector>#include <algorithm> Type definitions:typedef vector<int> IntVector;typedef IntVector::iterator IntVectorIt;//Allocate a vector of size n//Define an iterator and its initial and //terminal valuesIntVector Vector1(n);IntVectorIt start1,end1,it1;start1 = Vector1.begin();end1 = Vector1.end();//Enter n consecutive even numbers into vector 1int i=0;for(it1=start1;it1!=end1;it1++){ *it1=2*i;i++;}//Perform merging of vectors 1 and 2 into vector 3merge(start1,end1,start2,end2,start3);Sort Algorithm:void sort(RandomAccessIterator first, RandomAccessIterator last);Sort sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.//Enter random numbers into NumbersVectorfor(itv=startv;itv!=endv;itv++)*itv=rand()%n; //Start timing sorting procedureclock_t start=clock();sort(startv,endv);//Stop timing sorting procedureclock_t stop=clock();//Print out elapsed time in milisecondscout << "The elapsed time: ";cout << 1000*(stop-start) /CLOCKS_PER_SEC <<
View Full Document