C + + built-in functions

Source: Internet
Author: User

There are many built-in functions in C + +, of course, which can be defined in the program itself. In order to facilitate everyone (and I) to find how to use, in the problem of continuous accumulation, this article will be constantly updated.

  1.unique () de-weight function

Suppose there is an array a in the title, which requires a from a large to a small output, and the same element can only output one

Practice: Let's say that everyone knows the sort thing (if you don't know it, just sort it out), sort it out, make the elements in a certain order (from big to small, to large), and then:

    int length= Unique (/* is almost the same as in sort */)-array name;

    The length stored here is the size of the array after the de-

    The unique function does not delete the same number, but instead moves it to the last face of the array .

  2.sort () Quick Sort

    Sort a part of an array header file: algorithm

The core code (which is generally not required to be remembered, can be called directly):

voidSortint*a,intLintr) {//time-Complexity recursive T (n) = O (n) + 2*t (n)//i.e. O (NLOGN)Swap (A[l], A[rand () *rand ()% (r-l+1)+l]); intTMP = A[l];//the elements on the right are smaller than TMP, and the elements on the left are larger than TMP    intL_ = L, R_ =R;  while(L < R) {//the time complexity of the cycle is interval length         while(L <r) {if(A[r] > tmp) r--;//A[r] Fall in the right place, do not need to be adjusted            Else{a[l++] = A[r]; Break; } }         while(L <r) {if(A[l] < TMP) l++;//Ibid .            Else{a[r--] = a[l]; Break; } }} A[l]=tmp; if(L-l_ >1) sort (A, l_, L-1); if(R-r_ >1) sort (A, r+1, r_);}

   The sort function generally defaults to small to large, and requires a handwriting function when you need to sort the array from large to small or to sort the struct:

//array from large to small sortBOOLCMP1 (intXinty) {returnx > y;//sort from big to small//return x < y; Sort from small to large}//structure SortstructNode {intW, id;}; BOOLCMP2 (node x, node Y) {//the structure x, y according to W from large to small sort, when the value of x, Y and W are equal, according to the ID from small to big shot    if(X.W = = Y.W)returnX.id <y.id; returnX.W >Y.W;}

    Application of Sort: Quick selection

int Select(int*a,intLintRintk) {swap (a[l], A[rand ()*rand ()% (r-l+1)+l]); intTMP =A[l]; intL_ = L, R_ =R;  while(L <r) { while(L <r) {if(A[r] > tmp) r--;//A[r] Fall in the right place, do not need to be adjusted            Else{a[l++] = A[r]; Break; } }         while(L <r) {if(A[l] < TMP) l++;//Ibid .            Else{a[r--] = a[l]; Break; } }} A[l]=tmp; if(k = = l-l_+1)returnA[l]; if(K < l-l_+1)return Select(A, L_, L-1, k);//Compare the elements on the left of TMP    if(k > l-l_+1)return Select(A, r+1, R_, K (l-l_+1));//Compare the elements on the right side of TMP}

  3.lower_bound () and Upper_bound ()

Pending update ....

C + + built-in functions

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.