STL Common Algorithm (algorithms) Introduction (STL sorting algorithm, non-variable sequence queue) _c language

Source: Internet
Author: User

Algorithm: Used to process elements within a cluster. They can search, sort, modify, and use those elements for different purposes. An action or function that is applied to a container to handle its memory in various ways, such as sort (sort), copy (copy) ...

The algorithm is embodied by the template function, which is not a member function of the container class, is a stand-alone function, they can be used in STL container, and can be used in ordinary C + + arrays.

Header files: #include <algorithm>

There are 4 kinds of basic algorithms in the STL generic algorithm:

1 variable-order queue algorithm: Can change the data in the container;
2 non-variable sequence queue algorithm: processing the data in the container without changing them;
3 Sorting value algorithm: Inclusion of the value in the container sorting and merging algorithm, there are two fork search algorithm,
4 General numerical algorithm: This kind of algorithm is not many, involves the useful arithmetic operation in the specialized domain, the independent inclusion in the header file <numeric>.

STL algorithm is not only for STL containers, but also for the general container is applicable.

Copy Code code as follows:

A sequence-changing queue algorithm

#include <iostream>
#include <algorithm>
#include <iterator>

The output iterator is used below Ostream_iterator

using namespace Std;

int main (void)
{int arr0[6]= {1,12,3,2,1215,90};
int arr1[7];
int arr2[6]= {2,5,6,9,0,-56};

Copy an array aar to arr1
Copy (Arr0, (arr0+6), arr1);

cout<< "arr0[6] Copy to Arr1[7],now arr1:" <<endl;
for (int i=0; i<7; i++)
cout<< "" <<arr1[i];

The last array element is not assigned a value, so the output is a random number

Flip the sorted arr

Reverse (arr0,arr0+6);
cout<< "\ n" << "arr reversed, now arr:" <<endl;

Copy to output iterator
Copy (arr0,arr0+6,ostream_iterator<int> (cout, ""));

Exchange Arr0 and ARR2 sequences

Swap_ranges (ARR0,ARR0+6,ARR2);

cout<< "\ r" << "Arr0 swaped to ARR2, and now arr0 ' content:" <<endl;
Copy (arr0,arr0+6,ostream_iterator<int> (cout, ""));

cout<< "\ n" << "ARR2:" <<endl;
Copy (arr2,arr2+6,ostream_iterator<int> (cout, ""));

return 0;
}



Non-variable sequential queue algorithm (member statistic calculation, search matching)

Copy Code code as follows:

The algorithm of non-variable sequence queue

#include <iostream>
#include <vector>
#include <algorithm>

using namespace Std;

int main (void)
{int a[10]= {12,31,5,2,23,121,0,89,34,66};

Vector<int> v1 (a,a+10);
Vector<int>::iterator result1,result2; RESULT1 and RESULT2 are random access iterators

Result1=find (V1.begin (), V1.end (), 2); Find the 2,RESULT1 in V1 to point to 2 in V1

Result2=find (V1.begin (), V1.end (), 8); Not found in the V1 8,result2 is pointing to V1.end ()

Cout<<result1-v1.begin () <<endl; 3-0=3 or 4-1=3, the screen result is 3.
Cout<<result2-v1.end () <<endl; 10-10=0;

int b[9]= {5,2,23,54,5,5,5,2,2};

cout<< "a[10]={12,31,5,2,23,121,0,89,34,66};\n";
cout<< "b[9]={5,2,23,54,5,5,5,2,2};\n";

Vector<int> v2 (a+2,a+8);
Vector<int> v3 (b,b+4);

Result1=search (V1.begin (), V1.end (), V2.begin (), V2.end ());

cout<<*result1<<endl;
The sequence V2,RESULT1 pointed to the location where V2 started in the V1 in the V1

Result1=search (V1.begin (), V1.end (), V3.begin (), V3.end ());

cout<<* (result1-1) <<endl;
The sequence V3,result pointed to V1.end () was not found in the V1, and the screen prints out the last element of the V1 66

Vector<int> v4 (B,B+9);

int I=count (V4.begin (), V4.end (), 5);
int J=count (V4.begin (), V4.end (), 2);


cout<< "There are" <<i<< "members of V4 Equel to 5" <<endl;
cout<< "There are" <<j<< "members of V4 Equel to 2" <<endl;


Calculate how many members in the V4 are equal to 5, 2
return 0;
}



Sorting algorithm

Copy Code code as follows:

Sorting value algorithm

#include <iostream>
#include <algorithm>
using namespace Std;

int main (void)
{int a[10]= {12,0,5,3,6,8,9,34,32,18};
int b[5]= {5,3,6,8,9};
int d[15];

Sort (a,a+10);
cout<< "Sorted a[10]:";
for (int i=0; i<10; i++)
cout<<a[i]<< "";

Sort (b,b+5); 3 5 6 8 8

if (includes (a,a+10,b,b+5))//Whether an array contains another array
cout<< "\ n" << "sorted B are included in a." <<endl;
Else
cout<< "sorted a dosn ' t contain sorted b!";

Merge (A,a+10,b,b+5,d); Merge

cout<< "a[10]={12,0,5,3,6,8,9,34,32,18};\n";
cout<< "b[5]={5,3,6,8,9};\n";
cout<< "merge (a,a+10,b,b+5,d); \ n";
cout<< "then d[15]:\n";
for (int j=0; j<15; j + +)
cout<<d[j]<< "";
return 0;
}

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.