C++中STL的各类算法使用及实现总结

算法部分主要由头文件组成。
是所有STL头文件中最大的一个(尽管它很好理解),它是由一大堆模版函数组成的,可以认为每个函数在很大程度上都是独立的,其中常用到的功能范围涉及到比较、交换、查找、遍历操作、复制、修改、移除、反转、排序、合并等等。

体积很小,只包括几个在序列上面进行简单数学运算的模板函数,包括加法和乘法在序列上的一些操作。

STL中算法大致分为四类:
1)非可变序列算法:指不直接修改其所操作的容器内容的算法。
2)可变序列算法:指可以修改它们所操作的容器内容的算法。
3)排序算法:对序列进行排序和合并的算法、搜索算法以及有序序列上的集合操作。
4)数值算法:对容器内容进行数值计算。
以下对所有算法进行细致分类、标明功能并且实现:
 

#define SIZE 1000
#include
#include
#include
#include
#include
#include
using namespace std;

/***************************************1.查找*************************************************/
//在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的ForwardIterator,否则返回last
void Fadjacent_find(vectorvec){
    vec.push_back(999);
    vector::iterator it=adjacent_find(vec.begin(),vec.end());
    cout<<"The adjacent_find result:"<<*it<vec,int value){
    bool result=binary_search(vec.begin(),vec.end(),value);
    cout<<"The binary_search result:"<vec,int value){
    int result=count(vec.begin(),vec.end(),value);
    cout<<"The count result:"<vec){
    int result=count_if(vec.begin(),vec.end(),cmp1);
    cout<<"The count_if result:"<vec,int value){
    pair::iterator,vector::iterator> result=equal_range(vec.begin(),vec.end(),value);
    cout<<"The equal_range result:"<<*result.first<<"   "<<*result.second<vec,int value){
    vector::iterator result=find(vec.begin(),vec.end(),value);
    cout<<"The find result:"<<&*result<=996;
}
void Ffind_if(vectorvec){
    vector::iterator result=find_if(vec.begin(),vec.end(),cmp2);
    cout<<"The find_if result:"<<*result<vec,int value){
    vector::iterator result=lower_bound(vec.begin(),vec.end(),value);
    cout<<"The lower_bound result:"<<*result<vec,int value){
    vector::iterator result=upper_bound(vec.begin(),vec.end(),value);
    cout<<"The upper_bound result:"<<*result<vec(array,array+16);
    //search_n(s,e,cnt,num);找区间内[s,e)连续cnt个的num返回的是找到的第一个num的迭代器
    vector::iterator result=search_n(vec.begin(),vec.end(),2,996);
    for(int i=0;i vi{1,5,8,19,200,1,8,56,74,91,100,101,102,103};
    cout<<"before inplace_merge,vi=";
    for_each(vi.begin(),vi.end(),[](int i){cout<vec1{1,5,8,19,200};
    vectorvec2{1,8,56,74,91,100,101,102,103,996};
    vectorvec3(vec1.size()+vec2.size());
    merge(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),vec3.begin(),cmp4);
    cout<<"The merge result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    nth_element(vec.begin(),vec.end()-3,vec.end());
    cout<<"The nth_element result:"<b;
}
void Fpartial_sort(){
    vectorvec{995,1024,1,8,56,74,91,100,101,102,103,996};
    partial_sort(vec.begin(),vec.begin()+5,vec.end(),cmp5);
    cout<<"The partial_sort result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vectorvec1(vec.size());
    partial_sort_copy(vec.begin(),vec.end(),vec1.begin(),vec.end());
    cout<<"The  partial_sort_copy result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    partition(vec.begin(),vec.end(),cmp6);
    cout<<"The  partition result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    stable_partition(vec.begin(),vec.end(),cmp6);
    cout<<"The  stable_partition result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    random_shuffle(vec.begin(),vec.end());
    cout<<"The random_shuffle result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    reverse(vec.begin(),vec.end());
    cout<<"The reverse result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vectorvec1(vec.size());
    reverse_copy(vec.begin(),vec.end(),vec1.begin());
    cout<<"The reverse_copy result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    rotate(vec.begin(),vec.begin()+5,vec.end());
    cout<<"The rotate result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vectorvec1(vec.size());
    rotate_copy(vec.begin(),vec.begin()+5,vec.end(),vec1.begin());
    cout<<"The rotate_copy result:"<b;
}
void Fsort(){
    vectorvec{995,1024,1,8,56,74,91,100,101,102,103,996};
    sort(vec.begin(),vec.end(),cmp7);
    cout<<"The sort result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    stable_sort(vec.begin(),vec.end(),cmp7);
    cout<<"The stable_sort result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vectorvec1(vec.size());
    copy(vec.begin(),vec.end(),vec1.begin());
    cout<<"The copy result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vectorvec1(vec.size());
    copy_backward(vec.begin(),vec.end(),vec1.end());
    cout<<"The copy_backward result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    swap_ranges(vec.begin(),vec.begin()+vec.size()/2,vec.begin()+vec.size()/2);
    cout<<"The swap_ranges result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    cout<<"The remove result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vectorvec1(vec.size());
    remove_copy(vec.begin(),vec.end(),vec1.begin(),1024);
    cout<<"The remove_copy result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    remove_if(vec.begin(),vec.end(),cmp8);
    cout<<"The remove_if result:"<vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vectorvec1(vec.size());
    remove_copy_if(vec.begin(),vec.end(),vec1.begin(),cmp8);
    cout<<"The remove_copy_if result:"<vec{995,1024,1,1,1,8,56,74,91,100,8,996,1024,101,102,103,996};
    sort(vec.begin(),vec.end());
    cout<<"before unique:";
    for_each(vec.begin(),vec.end(),[](int i){cout<vec{1,2,3,4};
    sort(vec.begin(),vec.end());
    cout<<"The next_permutation result:"<b;
}
void Fprev_permutation(){
    vectorvec{1,2,3,4};
    sort(vec.begin(),vec.end(),cmp9);
    cout<<"The prev_permutation result:"<中,作用有两个,一个是累加求和,另一个是自定义类型数据的处理(需要仿函数)
void Faccumulate(){
    vectorvec{1,2,3,4};
    vectorvec1{"You","are","beautiful!"};
    int result=accumulate(vec.begin(),vec.end(),0);
    string str=accumulate(vec1.begin(),vec1.end(),string("*****"));
    cout<<"The accumulate result:"<vec{1,2,3,4};
    vectorvec1(vec.size());
    partial_sum(vec.begin(),vec.end(),vec1.begin());
    cout<<"The partial_sum result:"<vec{1,2,3,4};
    vectorvec1(vec);
    int result=inner_product(vec.begin(),vec.end(),vec1.begin(),0);
    cout<<"The inner_product result:"<vec{1,2,3,4};
    vectorvec1(vec.size());
    adjacent_difference(vec.begin(),vec.end(),vec1.begin());
    cout<<"The adjacent_difference result:"<vec(10);
    fill(vec.begin(),vec.end(),996);
    cout<<"The fill result:"<vec(10);
    fill_n(vec.begin(),5,996);
    cout<<"The fill_n result:"<vec(10);
    fill(vec.begin(),vec.end(),"996");
    cout<<"The for_each result:"<vec1(10);
    fill(vec1.begin(),vec1.end(),1024);
    vectorvec2(vec1);
    bool result=equal(vec1.begin(),vec1.end(),vec2.begin());
    cout<<"The equal result:"<vec1{1,2,3,4,5,6,7};
    vectorvec2{1,2,3,4,5};
    bool result=includes(vec1.begin(),vec1.end(),vec2.begin(),vec2.end());
    cout<<"The includes result:"<vec{995,1024,1,1,1,8,56,74,91,100,8,996,1024,101,102,103,996};
    vector::iterator result;
    result=max_element(vec.begin(),vec.end());
    cout<<"The max_element&&min_element result:"<vec1{1,2,3,4,5,6,7};
    vectorvec2{1,2,3,4,5,8};
    pair::iterator,vector::iterator>result;
    result=mismatch(vec1.begin(),vec1.end(),vec2.begin());
    cout<<"The mismatch result:"<vec1{1,2,3,4,5,6,7};
    vectorvec2{1,2,3,4,5,8};
    vectorvec;
    set_union(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),inserter(vec,vec.begin()));
    cout<<"The set_union result:"<vec{6, 1, 2, 5, 3, 4};
    cout<<"before make_heap result:"<vec;
    for(int i=0;i

运行结果如下:

The adjacent_find result:999
The binary_search result:1
The count result:1
The count_if result:999
The equal_range result:996   997
The find result:0x126a920
aacaabbcddefacss is: a
acaabbcddefacss is: a
caabbcddefacss is: c
aabbcddefacss is: a
abbcddefacss is: a
bbcddefacss is: b
bcddefacss is: b
cddefacss is: c
ddefacss is: d
defacss is: d
efacss is: e
facss is: f
acss is: a
css is: c
ss is: s
s is: s
The find_end result:acss is: a
The find_first_of result:a
The find_if result:996
The lower_bound result:996
The upper_bound result:997
The search result:c
vec[0].address:0x1261870 the num is:1
vec[1].address:0x1261874 the num is:996
vec[2].address:0x1261878 the num is:996
vec[3].address:0x126187c the num is:996
vec[4].address:0x1261880 the num is:5
vec[5].address:0x1261884 the num is:5
vec[6].address:0x1261888 the num is:996
vec[7].address:0x126188c the num is:996
vec[8].address:0x1261890 the num is:6
vec[9].address:0x1261894 the num is:6
vec[10].address:0x1261898 the num is:6
vec[11].address:0x126189c the num is:7
vec[12].address:0x12618a0 the num is:8
vec[13].address:0x12618a4 the num is:8
vec[14].address:0x12618a8 the num is:9
vec[15].address:0x12618ac the num is:10
The first search_n result:0x1261874  996
The second search_n result:0x1261874  996
before inplace_merge,vi=1 5 8 19 200 1 8 56 74 91 100 101 102 103
after inplace_merge,vi=1 1 5 8 8 19 56 74 91 100 101 102 103 200
The merge result:
1 1 5 8 8 19 56 74 91 100 101 102 103 200 996
The nth_element result:
103 102 1 8 56 74 91 100 101 995 996 1024
The partial_sort result:
1024 996 995 103 102 1 8 56 74 91 100 101
The  partial_sort_copy result:
1 8 56 74 91 100 101 102 103 995 996 1024
The  partition result:
996 1024 102 8 56 74 100 91 101 1 103 995
The  stable_partition result:
1024 8 56 74 100 102 996 995 1 91 101 103
The random_shuffle result:
103 1024 102 1 995 996 100 8 56 91 101 74
The reverse result:
996 103 102 101 100 91 74 56 8 1 1024 995
The reverse_copy result:
996 103 102 101 100 91 74 56 8 1 1024 995
The rotate result:
74 91 100 101 102 103 996 995 1024 1 8 56
The rotate_copy result:
74 91 100 101 102 103 996 995 1024 1 8 56
The sort result:
1024 996 995 103 102 101 100 91 74 56 8 1
The stable_sort result:
1024 996 995 103 102 101 100 91 74 56 8 1
The copy result:
995 1024 1 8 56 74 91 100 101 102 103 996
The copy_backward result:
995 1024 1 8 56 74 91 100 101 102 103 996
A.name=B A.score=2
B.name=A B.score=1
The iter_swap result:
996 1024 1 8 56 74 91 100 101 102 103 995
The swap_ranges result:
91 100 101 102 103 996 995 1024 1 8 56 74
The remove result:
before remove:The vec.size():12
995 1024 1 8 56 74 91 100 101 102 103 996
after remove:The vec.size():12
995 1024 8 56 74 91 100 101 102 103 996 996
The remove_copy result:
995 1 8 56 74 91 100 101 102 103 996 0
The remove_if result:
1024 8 56 74 100 102 996 100 101 102 103 996
The remove_copy_if result:
1024 8 56 74 100 102 996 0 0 0 0 0
before replace:You are beautiful!
after replace:$ou are beautiful!
before unique:1 1 1 8 8 56 74 91 100 101 102 103 995 996 996 1024 1024
after unique:1 8 56 74 91 100 101 102 103 995 996 1024 995 996 996 1024 1024
The next_permutation result:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
The prev_permutation result:
4 3 2 1
4 3 1 2
4 2 3 1
4 2 1 3
4 1 3 2
4 1 2 3
3 4 2 1
3 4 1 2
3 2 4 1
3 2 1 4
3 1 4 2
3 1 2 4
2 4 3 1
2 4 1 3
2 3 4 1
2 3 1 4
2 1 4 3
2 1 3 4
1 4 3 2
1 4 2 3
1 3 4 2
1 3 2 4
1 2 4 3
1 2 3 4
The accumulate result:
From 1 to 4:10
*****Youarebeautiful!
The partial_sum result:
1 3 6 10
The inner_product result:
30
The adjacent_difference result:
1 1 1 1
The fill result:
996 996 996 996 996 996 996 996 996 996
The fill_n result:
996 996 996 996 996 0 0 0 0 0
The for_each result:
996 996 996 996 996 996 996 996 996 996
The equal result:
first:1
second:0
The includes result:
first:1
second:0
The max&&min result:
The max:2
The min:1
The max_element&&min_element result:
The max:1024
The min:1
The mismatch result:
first:6
second:8
The set_union result:
1 2 3 4 5 6 7 8
The set_intersection result:
1 2 3 4 5
The set_difference result:
6 7
The set_symmetric_difference result:
6 7 8
before make_heap result:
6 1 2 5 3 4
After make_heap result:
6 5 4 1 3 2
After push_heap result:
999 5 6 1 3 2 4
After pop_heap result:
6 5 4 1 3 2 999
After sort_heap result:
1 2 3 4 5 999 6

 

你可能感兴趣的:(STL学习笔记,STL,STL算法实现,C++,学习笔记)