C++中STL学习笔记——常见算法操作演示

时间:2022-07-23
本文章向大家介绍C++中STL学习笔记——常见算法操作演示,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

常见STL算法操作演示

概述

STL提供了大约100个实现算法的模版函数,比如算法for_each将为指定序列中的每一个元素调用指定的函数,stable_sort以你所指定的规则对序列进行稳定性排序等等。只要我们熟悉了STL之后,许多代码可以被大大的化简,只需要通过调用一两个算法模板,就可以完成所需要的功能并大大地提升效率。

分类

STL中算法大致分为四类:

1.非可变序列算法:指不直接修改其所操作的容器内容的算法。

2.可变序列算法:指可以修改它们所操作的容器内容的算法。

3.排序算法:对序列进行排序和合并的算法、搜索算法以及有序序列上的集合操作。

4.数值算法:对容器内容进行数值计算。

下面的代码涉及到几乎全部的STL函数,是小编纯手敲写的,已经在VSCode运行。

#define SIZE 1000
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<string>
#include<numeric>
using namespace std;

/***************************************1.查找*************************************************/
//在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的ForwardIterator,否则返回last
void Fadjacent_find(vector<int>vec){
    vec.push_back(999);
    vector<int>::iterator it=adjacent_find(vec.begin(),vec.end());
    cout<<"The adjacent_find result:"<<*it<<endl;
}

//在有序序列中查找value,找到返回true
void Fbinary_search(vector<int>vec,int value){
    bool result=binary_search(vec.begin(),vec.end(),value);
    cout<<"The binary_search result:"<<result<<endl;
}

//利用等于操作符,把标志范围内的元素与输入值比较,返回相等元素个数。
void Fcount(vector<int>vec,int value){
    int result=count(vec.begin(),vec.end(),value);
    cout<<"The count result:"<<result<<endl;
}

//利用输入的操作符,对标志范围内的元素进行操作,返回结果为true的个数
bool cmp1(int num){
    return num%1000;
}
void Fcount_if(vector<int>vec){
    int result=count_if(vec.begin(),vec.end(),cmp1);
    cout<<"The count_if result:"<<result<<endl;
}

//功能类似equal,返回一对iterator,第一个表示lower_bound,第二个表示upper_bound
void Fequal_range(vector<int>vec,int value){
    pair<vector<int>::iterator,vector<int>::iterator> result=equal_range(vec.begin(),vec.end(),value);
    cout<<"The equal_range result:"<<*result.first<<"   "<<*result.second<<endl;
}

//利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的Iterator
void Ffind(vector<int>vec,int value){
    vector<int>::iterator result=find(vec.begin(),vec.end(),value);
    cout<<"The find result:"<<&*result<<endl;
}

//在指定范围内查找"由输入的另外一对iterator标志的第二个序列"的最后一次出现。找到则返回最后一对的第一个ForwardIterator,
//否则返回输入的"另外一对"的第一个ForwardIterator。重载版本使用用户输入的操作符代替等于操作。
void Ffind_end(){
    string str="aacaabbcddefacss";
    string substr="ac";
    string::iterator result=find_end(str.begin(),str.end(),substr.begin(),substr.end());
    for(int i=0;i<str.length();i++){
        cout<<&str[i]<<" is: "<<str[i]<<endl;
    }
    cout<<"The find_end result:"<<&*result<<" is: "<<*result<<endl;
}

//在指定范围内查找"由输入的另外一对iterator标志的第二个序列"中任意一个元素的第一次出现。
//第一个序列中的元素第一次出现在第二个序列中的元素
void Ffind_first_of(){
    string str="mnblkjhgfdsac";
    string substr="ca";
    string::iterator result=find_first_of(str.begin(),str.end(),substr.begin(),substr.end());
    cout<<"The find_first_of result:"<<*result<<endl;
}

//它接收一个函数对象的参数作为参数,并使用它来做更复杂的评价对象是否和给出的查找条件相符。
bool cmp2(int value){
    return value>=996;
}
void Ffind_if(vector<int>vec){
    vector<int>::iterator result=find_if(vec.begin(),vec.end(),cmp2);
    cout<<"The find_if result:"<<*result<<endl;
}

//返回的是第一个不小于给定元素value的位置
void Flower_bound(vector<int>vec,int value){
    vector<int>::iterator result=lower_bound(vec.begin(),vec.end(),value);
    cout<<"The lower_bound result:"<<*result<<endl;
}

//返回的是第一个大于给定元素value的位置
void Fupper_bound(vector<int>vec,int value){
    vector<int>::iterator result=upper_bound(vec.begin(),vec.end(),value);
    cout<<"The upper_bound result:"<<*result<<endl;
}

//search算法在一个序列中找另一个序列的第一次出现的位置
void Fsearch(){
    string str="mnblkjhgfdsca";
    string substr="ca";
    string::iterator result=search(str.begin(),str.end(),substr.begin(),substr.end());
    cout<<"The search result:"<<*result<<endl;
}

//search_n
bool cmp3(int i,int j){
    return i==j;
}
void Fsearch_n(){
    int array[]={1,996,996,996,5,5,996,996,6,6,6,7,8,8,9,10};
    vector<int>vec(array,array+16);
    //search_n(s,e,cnt,num);找区间内[s,e)连续cnt个的num返回的是找到的第一个num的迭代器
    vector<int>::iterator result=search_n(vec.begin(),vec.end(),2,996);
    for(int i=0;i<vec.size();i++){
        cout<<"vec["<<i<<"].address:"<<&vec[i]<<" "<<"the num is:"<<vec[i]<<endl;
    }
    cout<<"The first search_n result:"<<&*result<<"  "<<*result<<endl;
    //search_n(s,e,cnt,num,cmp);找区间内[s,e)连续cnt个的num符合cmp()条件的,返回的是找到的第一个num的迭代器
    result=search_n(vec.begin(),vec.end(), 3, 996, cmp3);
    cout<<"The second search_n result:"<<&*result<<"  "<<*result<<endl;
}

/***************************************2.排序*************************************************/
//inplace_merge(vi.begin(),vi.begin()+5,vi.end()),第一个参数为起始,最后一个为末尾,中间为第一个有序序列的元素个数
void Finplace_merge(){
    vector<int> 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<<i<<" ";});
    cout<<endl;
 
    inplace_merge(vi.begin(),vi.begin()+5,vi.end());
    cout<<"after inplace_merge,vi=";
    for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
 
}
/**merge函数的作用是:将两个已经排好序的序列合并为一个有序的序列。
 * 函数参数:merge(first1,last1,first2,last2,result,compare);
 * firs1t为第一个容器的首迭代器,last1为第一个容器的末迭代器;
 * first2为第二个容器的首迭代器,last2为容器的末迭代器;
 * result为存放结果的容器,comapre为比较函数(可略写,默认为合并为一个升序序列)。
 */
bool cmp4(int iter1,int iter2){
    return iter1<iter2;
}
void Fmerge(){
    vector<int>vec1{1,5,8,19,200};
    vector<int>vec2{1,8,56,74,91,100,101,102,103,996};
    vector<int>vec3(vec1.size()+vec2.size());
    merge(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),vec3.begin(),cmp4);
    cout<<"The merge result:"<<endl;
    for_each(vec3.begin(),vec3.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//寻找第k位置的元素  第一个参数为起始地址,第二个为k,第三个为末尾地址
void Fnth_element(){
    vector<int>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:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//排序出前k小的元素,第一个参数为起始地址,第二个k,最后一个为末位地址,cmp可以不写  默认升序
bool cmp5(int a,int b){
    return a>b;
}
void Fpartial_sort(){
    vector<int>vec{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:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//与partial_sort类似 只是复制进一个新的容器
void Fpartial_sort_copy(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vector<int>vec1(vec.size());
    partial_sort_copy(vec.begin(),vec.end(),vec1.begin(),vec.end());
    cout<<"The  partial_sort_copy result:"<<endl;
    for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//将元素按照某种规定划分为两个集合  分组结果是乱序
bool cmp6(int a){
    return a%2==0;
}
void Fpartition(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    partition(vec.begin(),vec.end(),cmp6);
    cout<<"The  partition result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//stable_partition函数和partition函数差不多,分组之后只是顺序并没有被打乱。
void Fstable_partition(){
    vector<int>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:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//用来对一个元素序列进行重新排序(随机的)
void Frandom_shuffle(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    random_shuffle(vec.begin(),vec.end());
    cout<<"The random_shuffle result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//将指定范围内元素重新反序排序
void Freverse(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    reverse(vec.begin(),vec.end());
    cout<<"The reverse result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//与reverse类似 增加了复制操作
void Freverse_copy(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vector<int>vec1(vec.size());
    reverse_copy(vec.begin(),vec.end(),vec1.begin());
    cout<<"The reverse_copy result:"<<endl;
    for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//将元素从mid截断  后面的放前面  前面的放后面
void Frotate(){
    vector<int>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:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//和rotate类似,多了复制操作
void Frotate_copy(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vector<int>vec1(vec.size());
    rotate_copy(vec.begin(),vec.begin()+5,vec.end(),vec1.begin());
    cout<<"The rotate_copy result:"<<endl;
    for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//sort
bool cmp7(int a,int b){
    return a>b;
}
void Fsort(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    sort(vec.begin(),vec.end(),cmp7);
    cout<<"The sort result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//stable_sort
void Fstable_sort(){
    vector<int>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:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

/***************************************3.删除*************************************************/
//复制序列
void Fcopy(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vector<int>vec1(vec.size());
    copy(vec.begin(),vec.end(),vec1.begin());
    cout<<"The copy result:"<<endl;
    for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//与copy相同,不过元素是以相反顺序被拷贝。切记:最后一个参数是新容器的尾地址
void Fcopy_backward(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vector<int>vec1(vec.size());
    copy_backward(vec.begin(),vec.end(),vec1.end());
    cout<<"The copy_backward result:"<<endl;
    for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//交换存储在两个对象中的值
struct stu{
    string name;
    float score;
};
void Fswap(){
    stu A,B;
    A.score=1;A.name="A";
    B.name="B";B.score=2;
    swap(A,B);
    cout<<"A.name="<<A.name<<" "<<"A.score="<<A.score<<endl;
    cout<<"B.name="<<B.name<<" "<<"B.score="<<B.score<<endl;
}

//swap函数有一个缺点,就是无法交换2个迭代器的值  iter_swap可以交换
void Fiter_swap(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    iter_swap(vec.begin(),vec.end()-1);
    cout<<"The iter_swap result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//交换两个区域中的数据  三个参数,第一个是第一个序列的起始,第二个是第一个序列的结束,第三个是第二个序列的起始
void Fswap_ranges(){
    vector<int>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:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

/**
 * 就像所有算法,remove接收指定它操作的元素区间的一对迭代器。
 * 它不接收一个容器,所以remove不知道它作用于哪个容器。
 * 此外,remove也不可能发现容器,因为没有办法从一个迭代器获取对应于它的容器。
 * 想想怎么从容器中除去一个元素。
 * 唯一的方法是调用那个容器的一个成员函数,几乎都是erase的某个形式,
 * (list有几个除去元素的成员函数不叫erase,但它们仍然是成员函数。)
 * 因为唯一从容器中除去一个元素的方法是在那个容器上调用一个成员函数,
 * 而且因为remove无法知道它正在操作的容器,所以remove不可能从一个容器中除去元素。
 * 这解释了另一个令人沮丧的观点——从一个容器中remove元素    不会改变容器中元素的   个数 
 * **********remove并不“真的”删除东西,因为它做不到。************
*/
void Fremove(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    cout<<"The remove result:"<<endl;
    cout<<"before remove:";
    cout<<"The vec.size():"<<vec.size()<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    remove(vec.begin(),vec.end(),1);
    cout<<"after remove:";
    cout<<"The vec.size():"<<vec.size()<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//附加拷贝操作,原理不再过多解释
void Fremove_copy(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vector<int>vec1(vec.size());
    remove_copy(vec.begin(),vec.end(),vec1.begin(),1024);
    cout<<"The remove_copy result:"<<endl;
    for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//附加判定,原理不再解释
bool cmp8(int i){
    return i%2!=0;
}
void Fremove_if(){
    vector<int>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:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//判定加拷贝
void Fremove_copy_if(){
    vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996};
    vector<int>vec1(vec.size());
    remove_copy_if(vec.begin(),vec.end(),vec1.begin(),cmp8);
    cout<<"The remove_copy_if result:"<<endl;
    for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//用法较多 此处仅用一种
//replace_copy  replace_if  replace_copy_if同上略过
void Freplace(){
    string str="You are beautiful!";
    cout<<"before replace:"<<str<<endl;
    str=str.replace(str.begin(),str.begin()+1,"$");
    cout<<"after replace:"<<str<<endl;
}

//清除序列中重复元素(需要提前有序,否则结果不符合预期),和remove类似,它也不能真正删除元素
//unique_copy不再赘述
void Funique(){
    vector<int>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<<i<<" ";});
    cout<<endl;
    unique(vec.begin(),vec.end());
    cout<<"after unique:";
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

/***************************************4.排列组合*************************************************/
//按照字典全排列 next_permutation()会取得[first,last)所标示之序列的下一个排列组合,如果没有下一个排列组合,便返回false;否则返回true
void Fnext_permutation(){
    vector<int>vec{1,2,3,4};
    sort(vec.begin(),vec.end());
    cout<<"The next_permutation result:"<<endl;
  do{
    for(int i=0;i<vec.size();i++)
        cout<<vec[i]<<" ";
    cout<<endl;
  }while(next_permutation(vec.begin(),vec.end()));
}

//同上
bool cmp9(int a,int b){
    return a>b;
}
void Fprev_permutation(){
    vector<int>vec{1,2,3,4};
    sort(vec.begin(),vec.end(),cmp9);
    cout<<"The prev_permutation result:"<<endl;
  do{
    for(int i=0;i<vec.size();i++)
        cout<<vec[i]<<" ";
    cout<<endl;
  }while(prev_permutation(vec.begin(),vec.end()));
}

/***************************************5.算术算法*************************************************/
//accumulate定义在#include<numeric>中,作用有两个,一个是累加求和,另一个是自定义类型数据的处理(需要仿函数)
void Faccumulate(){
    vector<int>vec{1,2,3,4};
    vector<string>vec1{"You","are","beautiful!"};
    int result=accumulate(vec.begin(),vec.end(),0);
    string str=accumulate(vec1.begin(),vec1.end(),string("*****"));
    cout<<"The accumulate result:"<<endl;
    cout<<"From 1 to 4:"<<result<<endl;
    cout<<str<<endl;
}

//partial_sum创建一个新序列,其中每个元素值代表指定范围内该位置前所有元素之和
void Fpartial_sum(){
    vector<int>vec{1,2,3,4};
    vector<int>vec1(vec.size());
    partial_sum(vec.begin(),vec.end(),vec1.begin());
    cout<<"The partial_sum result:"<<endl;
    for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//inner_product  第一参数起始,第二个终止,第三个起始,第四个内积和初始值  内积和为返回值
void Finner_product(){
    vector<int>vec{1,2,3,4};
    vector<int>vec1(vec);
    int result=inner_product(vec.begin(),vec.end(),vec1.begin(),0);
    cout<<"The inner_product result:"<<endl;
    cout<<result<<endl;
}

//adjacent_difference 创建一个新序列,新序列中每个新值代表当前元素与上一个元素的差
void Fadjacent_difference(){
    vector<int>vec{1,2,3,4};
    vector<int>vec1(vec.size());
    adjacent_difference(vec.begin(),vec.end(),vec1.begin());
    cout<<"The adjacent_difference result:"<<endl;
    for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

/***************************************6.生成和异变算法*************************************************/
//将输入值赋给标志范围内的所有元素
void Ffill(){
    vector<int>vec(10);
    fill(vec.begin(),vec.end(),996);
    cout<<"The fill result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//fill_n将输入值赋给first到first+n范围内的所有元素。
void Ffill_n(){
    vector<int>vec(10);
    fill_n(vec.begin(),5,996);
    cout<<"The fill_n result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

//for_each  用指定函数依次对指定范围内所有元素进行迭代访问,返回所指定的函数类型。该函数不得修改序列中的元素。
void Ffor_each(){
    vector<string>vec(10);
    fill(vec.begin(),vec.end(),"996");
    cout<<"The for_each result:"<<endl;
    for_each(vec.begin(),vec.end(),[](string i){cout<<i<<" ";});
    cout<<endl;
}

/***************************************7.关系算法*************************************************/
//equal 判断两个序列是否相同
void Fequal(){
    vector<int>vec1(10);
    fill(vec1.begin(),vec1.end(),1024);
    vector<int>vec2(vec1);
    bool result=equal(vec1.begin(),vec1.end(),vec2.begin());
    cout<<"The equal result:"<<endl;
    cout<<"first:"<<result<<endl;
    vec1.push_back(999);
    result=equal(vec1.begin(),vec1.end(),vec2.begin());
    cout<<"second:"<<result<<endl;
}

//includes判断第2个指定范围内的所有元素是否都被第1个范围包含,成功返回true
void Fincludes(){
    vector<int>vec1{1,2,3,4,5,6,7};
    vector<int>vec2{1,2,3,4,5};
    bool result=includes(vec1.begin(),vec1.end(),vec2.begin(),vec2.end());
    cout<<"The includes result:"<<endl;
    cout<<"first:"<<result<<endl;
    vec2.push_back(8);
    result=includes(vec1.begin(),vec1.end(),vec2.begin(),vec2.end());
    cout<<"second:"<<result<<endl;
}

//max min
void maxmin(){
    int a=1;
    int b=2;
    int result=max(a,b);
    cout<<"The max&&min result:"<<endl;
    cout<<"The max:"<<result<<endl;
    result=min(a,b);
    cout<<"The min:"<<result<<endl;
}

//max_element  min_element
void Fmaxandmin_element(){
    vector<int>vec{995,1024,1,1,1,8,56,74,91,100,8,996,1024,101,102,103,996};
    vector<int>::iterator result;
    result=max_element(vec.begin(),vec.end());
    cout<<"The max_element&&min_element result:"<<endl;
    cout<<"The max:"<<*result<<endl;
    result=min_element(vec.begin(),vec.end());
    cout<<"The min:"<<*result<<endl;
}

//mismatch 并行比较两个序列,指出第一个不匹配的位置,返回一对iterator,标志第一个不匹配元素位置。如果都匹配,返回每个容器的last。
void Fmismatch(){
    vector<int>vec1{1,2,3,4,5,6,7};
    vector<int>vec2{1,2,3,4,5,8};
    pair<vector<int>::iterator,vector<int>::iterator>result;
    result=mismatch(vec1.begin(),vec1.end(),vec2.begin());
    cout<<"The mismatch result:"<<endl;
    cout<<"first:"<<*result.first<<endl;
    cout<<"second:"<<*result.second<<endl;
}

/***************************************8.集合算法*************************************************/
//set_union构造一个有序序列,包含两个序列中所有的不重复元素  并集
//set_intersection构造一个有序序列,其中元素在两个序列中都存在  全集(可重复出现)
//set_difference构造一个有序序列,该序列仅保留第一个序列中存在的而第二个中不存在的元素  A减去交集
//set_symmetric_difference构造一个有序序列,该序列取两个序列的对称差集(并集-交集)
void Fset_unionset_intersectionset_differenceset_symmetric_difference(){
    vector<int>vec1{1,2,3,4,5,6,7};
    vector<int>vec2{1,2,3,4,5,8};
    vector<int>vec;
    set_union(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),inserter(vec,vec.begin()));
    cout<<"The set_union result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    vec.resize(0);
    set_intersection(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),inserter(vec,vec.begin()));
    cout<<"The set_intersection result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    vec.resize(0);
    set_difference(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),inserter(vec,vec.begin()));
    cout<<"The set_difference result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    vec.resize(0);
    set_symmetric_difference(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),inserter(vec,vec.begin()));
    cout<<"The set_symmetric_difference result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}

/***************************************9.堆算法*************************************************/
/**make_heap: 根据指定的迭代器区间以及一个可选的比较函数,来创建一个heap. O(N)
 * push_heap: 把指定区间的最后一个元素插入到heap中. O(logN)
 * pop_heap: 弹出heap顶元素, 将其放置于区间末尾. O(logN)
 * sort_heap:堆排序算法,通常通过反复调用pop_heap来实现. N*O(logN)
 */
void Fheap(){
    vector<int>vec{6, 1, 2, 5, 3, 4};
    cout<<"before make_heap result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    make_heap(vec.begin(),vec.end());
    cout<<"After make_heap result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    vec.push_back(999);
    push_heap(vec.begin(),vec.end());
    cout<<"After push_heap result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    pop_heap(vec.begin(),vec.end());
    cout<<"After pop_heap result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    sort_heap(vec.begin(),vec.end());
    cout<<"After sort_heap result:"<<endl;
    for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
}
int main(){
    vector<int>vec;
    for(int i=0;i<SIZE;i++){
        vec.push_back(i);
    }
    //1.查找算法(13个):判断容器中是否包含某个值
    Fadjacent_find(vec);
    Fbinary_search(vec,1);
    Fcount(vec,1);
    Fcount_if(vec);
    Fequal_range(vec,996);
    Ffind(vec,996);
    Ffind_end();
    Ffind_first_of();
    Ffind_if(vec);
    Flower_bound(vec,996);
    Fupper_bound(vec,996);
    Fsearch();
    Fsearch_n();

    //2.排序和通用算法(14个):提供元素排序策略
    Finplace_merge();
    Fmerge();
    Fnth_element();
    Fpartial_sort();
    Fpartial_sort_copy();
    Fpartition();
    Fstable_partition();
    Frandom_shuffle();
    Freverse();
    Freverse_copy();
    Frotate();
    Frotate_copy();
    Fsort();
    Fstable_sort();

    //3.删除和替换算法(15个)
    Fcopy();
    Fcopy_backward();
    Fswap();
    Fiter_swap();
    Fswap_ranges();
    Fremove();
    Fremove_copy();
    Fremove_if();
    Fremove_copy_if();
    Freplace();
    Funique();

    //4.排列组合算法(2个)
    Fnext_permutation();
    Fprev_permutation();

    //5.算术算法(4个)
    Faccumulate();
    Fpartial_sum();
    Finner_product();
    Fadjacent_difference();

    //6.生成和异变算法(6个)generate generate_n transform暂不实现
    Ffill();
    Ffill_n();
    Ffor_each();

    //7.关系算法(8个)lexicographical_compare暂不实现 
    Fequal();
    Fincludes();
    maxmin();
    Fmaxandmin_element();
    Fmismatch();

    //8.集合算法
    Fset_unionset_intersectionset_differenceset_symmetric_difference();//四合一

    //9.堆算法
    Fheap();
    getchar();
    return 0;
}

运行结果如下:

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:0x3a920
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:0x31870 the num is:1
vec[1].address:0x31874 the num is:996
vec[2].address:0x31878 the num is:996
vec[3].address:0x3187c the num is:996
vec[4].address:0x31880 the num is:5
vec[5].address:0x31884 the num is:5
vec[6].address:0x31888 the num is:996
vec[7].address:0x3188c the num is:996
vec[8].address:0x31890 the num is:6
vec[9].address:0x31894 the num is:6
vec[10].address:0x31898 the num is:6
vec[11].address:0x3189c the num is:7
vec[12].address:0x318a0 the num is:8
vec[13].address:0x318a4 the num is:8
vec[14].address:0x318a8 the num is:9
vec[15].address:0x318ac the num is:10
The first search_n result:0x31874  996
The second search_n result:0x31874  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