STL algorithm part of the original tutorial, to detailed information please Baidu

Source: Internet
Author: User
Tags set set

STL algorithm Section

Library for algorithm algorithm library functional functional Programming Numeric basic numerical algorithm

One by one: find Find class has 13 functions, details please Baidu.

Using the method, S.find ("a"); s is a data type, such as set set, that is, to find the first and the same character in a collection, and then return to his iterator,

Iterators can be roughly understood as pointers.

Usage is queue<int>:: iterator it;

Because most functions return pointers to it types, all must be carefully mastered.

Two: Sorting and general algorithms

The sort sorting class has 14 functions. For details, please Baidu.

Sort (a,a+n) is directly lined up in this range.

or write your own CMP (Compare) function

Sort (A,A+N,CMP);

How to write about CMP, this is more troublesome,

Reverse sorts the elements in the specified range in reverse order.

1 Sorting one-dimensional arrays

Super simple notation

Bool cmp (int a,int b)

{return a>b;} descending sort, followed by complete notation

If it's qsort, you have to write that.

Qsort (A,n,sizeof (a[0]), CMP);

the int cmp (const void *a,const void *B)//const immutable value of a that points to void is polymorphic.

{return * (int *) a-* (int *) b;} From small to large (int *) is a pointer type that forces the type to be converted to int,
int cmp (const void *a,const void *b)//front * Good fetch pointer value to be reduced though troublesome,

{return * (int *) b-* (int *) A; }//from large to small but designed for versatility,

. Special case Sort double type (can only be written in this way):

Double in "1000";
int cmp (const void *a,const void *b)
{
Return (* (double*) a) > (* (double*) b)? 1:-1;

Problem with return value, CMP is int type, avoid double return decimal and be lost,
}

Three: Delete and replace type algorithm 15.

Copy replication sequence

Remove removes the element that is within the specified range, equal to the specified element.

Replace replaces the specified range element with replace

Swap, stored in the value of two objects.

Unique clears the repeating element within the specified range.

Four: permutation and combination algorithm of 2

Next_permutatiom reorder the current range to be full, getting the next sequence

Prevp_ermutatiom Ibid., got the last sequence.
Next_permutation (A,A+M) The full rank of the next

Examples are

#include <iostream>

#include <string>

#include <algorithm>

UsingNamespace std;

Intmain ()

{

int n,a[9]={1,2,3,4,5,6,7,8,9};

CIN >> N;

while (n--)

{int m;

CIN >> m;//Get a full array of numbers

Do

{

for (int i = 0; i < m; i++)

cout << A[i]; Output one line

cout<<endl;

}

while (Next_permutation (a,a+m)), gets the next fully ordered sequence.

}

return 0;

}

Five: Arithmetic algorithm of 4

Accumulate the sequence segments labeled by the iterator.

Adjacent_difference create a new sequence in which each new value represents the difference between the current element and the previous element.

This is good to know, there is no need to study deeply.

Six generation and variation algorithms, 6

I do not understand this, and the filling element is related.

Seven, relational algorithms, 8 (more important)

Max min This returns the big and the small.

Max_element Min_element returns the largest and smallest of the sequences. The return value is an iterator.

Equal determines if the value is equal within the flag range, and the return is true

Includes determines whether elements in the first specified range are included in the second range

Mismatch parallel comparison of two sequences indicates the first mismatched position. The return value is a pair of iterators.

Eight: Set algorithm of 4

Set_union constructs an ordered sequence that contains all the non-repeating elements in two sequences.

Set_intersection constructs an ordered sequence that finds intersecting elements of a set part

Set_difference constructs an ordered sequence that finds the first existence, and the second one does not exist.

Set_symmetric_difference constructs an ordered sequence that goes to the symmetric difference set of two sequences, which is the set-intersection

Nine: Heap Algorithm of 4

Don't understand. Check it out for yourself.

STL algorithm part of the original tutorial, to detailed information please Baidu

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.