c++STL排序及相关操作


1.排序

①sort

原形:template<classRanIt>


    void sort(RanItfirst,RanItlast); 


    template<class RanIt,classPred> 


   void sort(RanItfirst,RanItlast,Predpr);


第一个模板函数[first,last)间迭代器指示的元素数据按升序排列,第二个模板函数定义了比较函数pr(x,y)代替了operator<(x,y),功能是相似的,属于不稳定排序。

②stable_sort

原型:template<classRanIt>

         voidstable_sort(RanItfirst,RanItlast)

          template<classRanIt,classPred>

         voidstable_sort(RanItfirst,RanItlast,Predpr);

第一个sort函数[first,last)间迭代器指示的元素数据按升序排列,第二个sort函数定义了比较函数pr(x,y)代替了operator<(x,y),功能是相似的。与sort函数相

比,和它的名字一样,属于稳定排序。

③partial_sort


原型:template<classRanIt>


      voidpartial_sort(RanIt first,RanIt middle,RanIt last);


           template<classRanIt, classPred>


        voidpartial_sort(RanIt first,RanIt middle,RanIt last,Pred pr);


该函数实现了局部元素排序功能。对[first, last)间的元素排序结束后,仅前middle-first-1个元素是必须按要求排好序的,其它元素不一定是排好序

的。即:对任N[0,middle-first]M(N,last-first),都有*(first+N)<*(first+M)。第二个函数与第一个函数相比定义了比较函pr(x,y)代替了

operator<,功能是相似的。


④partial_sort_copy


原型:template<classInIt, classRanIt>


       RanItpartial_sort_copy(InIt first1,InIt last1,RanIt first2,RanIt last2);


  template<class InIt, class RanIt, class Pred>


         RanItpartial_sort_copy(InIt first1,InIt last1,RanIt first2,RanIt last2,Pred pr);


该函数功能是:与partial_sort相比有两点主要不同:


(1)排序结果可以输出到另外一个容器(当然也可自身容器);


(2)partial_sort函数中直接给出了middle值,而该函数middle值是计算出来的。middle=min(last1-first1,last2-first2)+first1之后:对任

N[0,middle-first1]M(N,last1-first1),都有*(first2+N)<*(first1+M)。第二个函数与第一个函数相比定义了比较函数pr(x,y)代替了operator<,

能是相似的。

//利用sort对整型向量升序排序
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
	int a[] = {1,8,6,10,4};
	vector<int> v(a, a+5);
	sort(v.begin(), v.end());
	cout << "升序排序结果是: " << endl;
	copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\t"));//1 4 6 8 10
	return 0;
}


//对学生成绩进行排序
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class Student
{
public:
    int NO;
    string name;
    int grade;
    Student(int NO,string name,int grade):NO(NO),name(name),grade(grade){}
   bool operator<(const Student &s)const
    {
        return grade<s.grade;
    }
};
ostream &operator<<(ostream &os,const Student &s)
{
    os<<s.NO<<"\t"<<s.name<<"\t"<<s.grade;
    return os;
}
bool com(const Student &s1, const Student &s2)
{
    if(s1.grade==s2.grade)
    {
        return s1.NO<s2.NO;
    }
    return s1.grade<s2.grade;
}
int main()
{
    Student s1(101,"ads",90);
    Student s2(102,"gre",80);
    Student s3(103,"utre",80);
    Student s4(104,"aaf",65);
    
    vector<Student>v;
    v.push_back(s1);
    v.push_back(s2);
    v.push_back(s3);
    v.push_back(s4);
    
    sort(v.begin(), v.end(),com);
    copy(v.begin(), v.end(), ostream_iterator<Student>(cout,"\n"));
    cout<<endl;
    return 0;
}

//利用partial_sort取整型向量最小的4个元素
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    int a[]={10,1,3,9,7,6,2,4,5,8};
    vector<int>v(a,a+10);
    copy(v.begin(), v.end(), ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    partial_sort(v.begin(), v.begin()+4, v.end());
    cout<<"前4个元素按升序排列:"<<endl;
    copy(v.begin(), v.end(), ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    
    cout<<"前4个元素按降序排列:"<<endl;
    partial_sort(v.begin(), v.begin()+4, v.end(), greater<int>());
    copy(v.begin(), v.end(), ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    return 0;
}

//求成绩最好的3位学生
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;
class Student
{
public:
    int NO;;
    string name;
    int grade;
    Student(){}
    Student(int NO,string name,int grade):NO(NO),name(name),grade(grade){}
    bool operator>(const Student &s)const
    {
        return grade>s.grade;
    }
};

ostream &operator<<(ostream &os,const Student &s)
{
    os<<s.NO<<"\t"<<s.name<<"\t"<<s.grade<<endl;
    return os;
    
}

int main()
{
    vector<Student>v;
    Student s1(101, "aaa",90);
    Student s2(102,"bbb",80);
    Student s3(103, "ccc", 60);
    Student s4(104, "ddd", 96);
    Student s5(105, "eee", 95);
    Student s6(106, "fff", 78);
    Student s7(107, "ggg", 94);
    Student s8(108, "hhh",92);
    v.push_back(s1);
    v.push_back(s2);
    v.push_back(s3);
    v.push_back(s4);
    v.push_back(s5);
    v.push_back(s6);
    v.push_back(s7);
    v.push_back(s8);
    
    vector<Student>result(3);
    partial_sort_copy(v.begin(), v.end(), result.begin(), result.end(),greater<Student>());
    
    for (int i=0; i<result.size(); i++)
    {
        Student &s=result.at(i);
       cout<<s<<endl;
    }
    return 0;
}

//list容器排序
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
    list<int>v;
    int x;
    for (int i=0; i<6; i++)
    {
        cin>>x;
        v.insert(v.begin(),x);
    }
  //  sort(v.begin(), v.end());//错误的
    v.sort();
   // v.sort(greater<int>());
    copy(v.begin(), v.end(), ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    return 0;
}

list容器不能用sort通用排序算法。这是由于sort需要的是随机迭代器,方便排序算法中的数据交换,而list提供的仅是双向迭代器。因此要想排序,只能用

list类本身提供的sort函数

2.第n个元素

nth_element

原型:template<classRanIt>

       voidnth_element(RanItfirst,RanItnth,RanItlast); 

        template<classRanIt,classPred> 

        voidnth_element(RanItfirst,RanItnth,RanItlast,Predpr);

该函数的功能是:在[first,last)指示的元素中,找第n个满足条件的元素,结果反映在RanItnth表示的迭代器指针中。例如:班上有10个学生,我想知道分数

排在倒数第4名的学生。如果要满足上述需求,可以用sort排好序,然后取第4位(因为是由小到大排),更聪明的会用partial_sort,只排前4位,然后得到第4

位。其实这时你还是浪费,因为前两位你根本没有必要排序,此时,你就需要nth_element。两个函数功能相近,第一个默认重载operate<,第2个可定义二

元函数对象。

<span style="font-size:14px;">//求成绩最好的3位学生
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;
class Student
{
public:
    int NO;;
    string name;
    int grade;
    Student(){}
    Student(int NO,string name,int grade):NO(NO),name(name),grade(grade){}
    bool operator>(const Student &s)const
    {
        return grade>s.grade;
    }
};

ostream &operator<<(ostream &os,const Student &s)
{
    os<<s.NO<<"\t"<<s.name<<"\t"<<s.grade<<endl;
    return os;
    
}

int main()
{
    vector<Student>v;
    Student s1(101, "aaa",90);
    Student s2(102,"bbb",80);
    Student s3(103, "ccc", 60);
    Student s4(104, "ddd", 96);
    Student s5(105, "eee", 95);
    Student s6(106, "fff", 78);
    Student s7(107, "ggg", 94);
    Student s8(108, "hhh",92);
    v.push_back(s1);
    v.push_back(s2);
    v.push_back(s3);
    v.push_back(s4);
    v.push_back(s5);
    v.push_back(s6);
    v.push_back(s7);
    v.push_back(s8);
    
    vector<Student>result(3);
    partial_sort_copy(v.begin(), v.end(), result.begin(), result.end(),greater<Student>());
    
    for (int i=0; i<result.size(); i++)
    {
        Student &s=result.at(i);
       cout<<s<<endl;
    }
    return 0;
}</span>

3.二分检索

①lower_bound

原型: template<class FwdIt,class T>

   FwdItlower_bound(FwdIt first, FwdIt last, const T& val);

 template<class FwdIt,class T, class Pred>

     FwdIt lower_bound(FwdIt first, FwdIt last, const T& val, Pred pr);

该函数功能是:容器元素已经排好序, 在[0, last-first)范围内寻找位置N, 对第1个模板函数而言:*(first+M) < val是true, *(first+N)>=val,也就是说在有序容

器中寻找第1个大于等于val值的位置,若找到返回first+N,否则返回last;对第2个模板函数而言功能相似,返回第一个不满足pr(*(first+M),val)的位置N。

②upper_bound

原型:template<class FwdIt,class T>

     FwdIt upper_bound(FwdIt first, FwdIt last, const T& val);

     template<class FwdIt, class T, class Pred>

       FwdItupper_bound(FwdIt first, FwdIt last, const T& val, Pred pr);

该函数功能是: 容器元素已经排好序, 在[0, last-first)范围内寻找位置N, 对第1个模板函数而言:*(first+M)  val是true, *(first+N)>val,

也就是说在有序容器中寻找第1个大于val值的位置,若找到返回first+N,否则返回last;对第2个模板函数而言功能相似,

返回第一个不满足pr(*(first+M),val)的位置N。

③equal_range

原型:template<classFwdIt,class T>       


        pair<FwdIt,FwdIt>equal_range(FwdItfirst,FwdItlast,constT&val); 


        template<class FwdIt,class T, classPred>       


       pair<FwdIt,FwdIt>equal_range(FwdItfirst,FwdItlast,constT&val,Predpr);


第1个模板函数功能是:在有序元素容器中,找出一对迭代指针midstart,midend,其间的元素都等于val。

即:*(midstart+N)=val。如果有结果,结果保存在pair对象中,相当于pair(lower(first,last,val),upper(first,last,val))。


第2个模板函数与第1个模板函数功能相近:找出一对迭代指针midstart,midend,其间元素:pr(*(midstart+N),val)都是true。


④binary_search

原型:template<classFwdIt,class T>


           boolbinary_search(FwdItfirst,FwdItlast,constT&val);


         template<class FwdIt,class T, classPred>


        boolbinary_search(FwdItfirst,FwdItlast,constT&val,Predpr);


第1个模板函数是在有序容器中查询*[first,last)间有无元素值等于val,若有返回true,若无返回false;


第2个模板函数是在有序容器中查询*[first,last)间有无元素值,满足:若pr(*(first+N),val)成立则返回true,若无返回false.


#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main()
{
    int a[]={1,2,2,3,3,3,4,4,4,4};
    list<int>L(a,a+10);
    copy(L.begin(), L.end(), ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    bool bExist=binary_search(L.begin(), L.end(), 5);
    cout<<"有元素5吗?:"<<(bExist==0?"false":"true")<<endl;
    list<int>::iterator first2=lower_bound(L.begin(), L.end(), 2);
    if(first2!=L.end())
    {
        cout<<"第一个元素2的位置:"<<distance(L.begin(),first2)<<endl;
    }
    list<int>::iterator last3=upper_bound(L.begin(), L.end(), 3);
    if(last3 != L.end())
    {
        cout<<"最后一个元素3的位置:"<<distance(L.begin(),--last3)<<endl;
    }
    pair<list<int>::iterator,list<int>::iterator>p=equal_range(L.begin(), L.end(), 4);
    if(p.first!=L.end())
    {
        long size=distance(p.first, p.second);
        cout<<"共有元素4的个数:"<<size<<endl;
        
    }
    return 0;
}


4. 归并

①merge

原型:template<class InIt1, class InIt2,classOutIt>

     OutIt merge(InIt1 first1, InIt1 last1,InIt2first2, InIt2 last2, OutIt x);

 template<class InIt1, class InIt2, classOutIt,classPred>

     OutIt merge(InIt1 first1, InIt1 last1,InIt2first2, InIt2 last2, OutIt x,Predpr);

第1个模板函数:两个有序容器元素(均按operator<排序)交替比较,按operate<排序后,输出至x迭代器表示的容器中。另当两个容器中比较元素相同,

则第一个容器中元素先输出到x代表的容器中。若两个容器共有 N个元素合并,则返回x+N。第二个函数与第1个函数功能相近,只是用二元函数pr代替了

operate<。

②inplace_merge

原型:template<classBidIt>

     void inplace_merge(BidIt first, BidIt middle,BidIt last);

template<class BidIt,classPred>

     void inplace_merge(BidIt first, BidIt middle,BidIt last,Predpr);

第一个模板函数功能是:一个容器分为两部分[first,middle)、[middle,last),每部分都已按operator<排好序,但整体不一定排好序。当进行inplace_merge

时,[first,middle)中指向元素交替与[middle,last)指向元素按operate<排序后,放入原容器中。当比较元素相同时,[first,middle)间元素优先存放。第二个

函数与第1个函数功能相近,只是用二元函数pr代替了operate<。

//归并函数简单示例
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int a[]={1,3,5,7};
    int b[]={2,4,6,8};
    int result[8];
    copy(a, a+4,ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    copy(b, b+4, ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    merge(a, a+4, b, b+4, result);
    copy(result, result+8, ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    
    int c[8]={1,3,4,8,2,5,6,7};
    copy(c, c+8, ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    inplace_merge(c, c+4, c+8);
    copy(c, c+8, ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    
    int a2[]={7,5,3,1};
    int b2[]={8,6,4,2};
    int result2[8];
  //  merge(a,a+4, b,b+4,result2);
    merge(a2,a2+4, b2,b2+4,result2,greater<int>());
    copy(result2, result2+8, ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    return  0;
}

5.序结构上的集合操作

①includes

原型: template<class InIt1, classInIt2>

    bool includes(InIt1 first1, InIt1 last1,InIt2first2, InIt2 last2);

template<class InIt1, class InIt2,classPred>

    bool includes(InIt1 first1, InIt1 last1,InIt2first2, InIt2 last2,Predpr);

第1个模板函数功能是:两个容器按operate<已排好序,若容器[first2, last2)指向的每个元素都在[first1,last1)指向的元素范围内,则[first1,last1)代表容器包含

[first2, last2)代表的容器。第2个模板函数功能与第一个功能相近:两个容器按pr已排好序,若容器[first2, last2)指向的每个元素都在[first1,last1)指向的元素

范围内,则[first1,last1)代表容器包含[first2, last2)代表的容器。

set_union

原型:template<class InIt1, class InIt2,classOutIt>

   OutItset_union(InIt1 first1, InIt1 last1,InIt2 first2,InIt2 last2, OutItx);

 template<class InIt1, class InIt2, classOutIt,classPred>

    OutItset_union(InIt1 first1, InIt1 last1,InIt2 first2,InIt2 last2,OutItx,Predpr);

第1个模板函数功能是:两个容器[first1,last1)、[first2,last2)均按operate<排好序,两个容器元素交替比较,小的值进入输出容器小x中,若比较两值等,

则取[first1,last1)中的元素。如果有N个元素拷贝到x代表的容器中,则返回x+N。第2个模板函数功能与第1个函数功能相近,只不过用二元函数pr代

替了operate<。

③set_intersection

原型:template<classInIt1, class InIt2, classOutIt>


   OutItset_intersection(InIt1first1, InIt1 last1,InIt2 first2, InIt2 last2, OutItx);


       template<class InIt1, class InIt2, classOutIt,classPred>


     OutItset_intersection(InIt1first1, InIt1 last1,InIt2 first2, InIt2 last2, OutItx,Predpr);


第1个模板函数功能是:两个容器[first1,last1)、[first2,last2)均按operate<排好序,两个容器元素交替比较,若比较两值相等,

则取、[first1,last1)中的元素。如果有N个元素拷贝到x代表的容器中,则返回x+N


第2个模板函数功能与第1个函数功能相近,只不过用二元函数pr代替了operate<。

④set_difference


原型:template<classInIt1, class InIt2, classOutIt>


     OutItset_difference(InIt1first1, InIt1 last1,InIt2 first2, InIt2 last2, OutItx);


         template<class InIt1, class InIt2, classOutIt,classPred>


     OutItset_difference(InIt1first1, InIt1 last1,InIt2 first2, InIt2 last2, OutItx,Predpr);


第1个模板函数功能是:两个容器[first1,last1)、[first2,last2)均按operate<排好序,两个容器元素交替比较,若比较两值相等,则取[first1,last1)中的元素。

如果有N个元素拷贝到x代表的容器中,则返回x+N


第2个模板函数功能与第1个函数功能相近,只不过用二元函数pr代替了operate<。

⑤set_symmetric_difference


原型:template<classInIt1, class InIt2, classOutIt>


   OutItset_symmetric_difference(InIt1first1, InIt1 last1, InIt2 first2, InIt2 last2, OutItx);


 template<class InIt1, class InIt2, classOutIt,classPred>


     OutItset_symmetric_difference(InIt1first1, InIt1 last1,InIt2 first2, InIt2 last2, OutItx,Predpr);


//集合操作简单示例
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
    int a[]={1,2,3,4,5};     //a b 各自中不能有重复元素  如int a[]={1,2,2,4,5};
    int b[]={1,2,3,6};
    list<int>L1(a,a+5);
    list<int>L2(b,b+5);
    cout<<"原始L1:"<<endl;
    copy(a, a+5, ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    cout<<"原始L2:"<<endl;
    copy(b, b+4, ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    
    //包含
    bool bRet=includes(L1.begin(), L1.end(), L2.begin(), L2.end());
    cout<<"L1包含L2?"<<(bRet==1? "  Yes":"   No")<<endl;
    //L1并L2
    
    list<int>L3;
    set_union(L1.begin(), L1.end(), L2.begin(), L2.end(), back_inserter(L3));
    cout<<"L1并L2:"<<endl;
    copy(L3.begin(), L3.end(), ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    //L1交L2
    list<int>L4;
    set_intersection(L1.begin(), L1.end(), L2.begin(), L2.end(), back_inserter(L4));
    cout<<"L1交L2:"<<endl;
    copy(L4.begin(), L4.end(), ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    //L1差L2
    list<int>L5;
    set_difference(L1.begin(), L1.end(), L2.begin(), L2.end(), back_inserter(L5));
    cout<<"L1差L2:"<<endl;
    copy(L5.begin(), L5.end(), ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    //L1对称差L2
    list<int>L6;
    set_symmetric_difference(L1.begin(), L1.end(), L2.begin(), L2.end(), back_inserter(L6));
    cout<<"L1对称差L2:"<<endl;
    copy(L6.begin(), L6.end(), ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    
    //L1mergeL2
    list<int>L7;
    merge(L1.begin(), L1.end(), L2.begin(), L2.end(), back_inserter(L7));
    cout<<"L1mergeL2:"<<endl;
    copy(L7.begin(), L7.end(), ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    return 0;
}

//学生集合判断
#include <iostream>
#include <algorithm>
using namespace std;
struct Student
{
    int NO;
    char name[20];
    bool operator<(const Student &s)const
    {
        return NO<s.NO;
    }
};
int main()
{
    bool bRet=true;
    Student s1[]={{1001,"zhang"},{1003,"li"},{1004,"wang"},{1002,"zhao"}};
    Student s2[]={{1001,"zhang"},{1004,"wang"},{1002,"zhao"}};
    sort(s1,s1+4);
    sort(s2, s2+3);
    bRet=includes(s1, s1+4, s2, s2+3);
    cout<<"s1包含s2?"<<endl;
    cout<<(bRet==true? "YES":"NO")<<endl;
    return 0;
}

6.最大和最小

①min

原型:template<class T>

    const T& min(const T& x, const T& y);

         template<class T, class Pred>

     const T& min(const T& x, const T& y, Pred pr);

第1个模板函数功能是:按operator<返回两个元素中较小的一个。第2个函数与第1个函数功能相近,只不过用二元比较函数pr代替了operator<。

②max

原型:template<class T>

    const T& max(const T& x, const T& y);

         template<class T, class Pred>

     const T& max(const T& x, const T& y, Pred pr);

第1个模板函数功能是:按operator<返回两个元素中较大的一个。第2个函数与第1个函数功能相近,只不过用二元比较函数pr代替了operator<。

③min_element

原型:template<classFwdIt>


          FwdItmin_element(FwdItfirst, FwdItlast);


          template<class FwdIt,class Pred>


     FwdItmin_element(FwdItfirst, FwdItlast, Predpr);


第1个模板函数功能是:按operator<比较,找出[first,last)指向元素的最小值,若设第N个元素是最小值,则返回first+N。


第2个函数与第1个函数功能相近,只不过用二元比较函数pr代替了operator<。

④max_element


原型:template<classFwdIt>


     FwdItmax_element(FwdItfirst, FwdItlast);


          template<class FwdIt,class Pred>


     FwdItmax_element(FwdItfirst, FwdItlast, Predpr);


第1个模板函数功能是:按operator<比较,找出[first,last)指向元素的最大值,若设第N个元素是最大值,则返回first+N。


第2个函数与第1个函数功能相近,只不过用二元比较函数pr代替了operator<。


//最大和最小函数简单示例
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int a=min(10,20);
    int b=max(10,20);
    cout<<a<<"\t"<<b<<endl;
    int c[]={1,10,5,7,9};
    copy(c,c+5,ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    int *it_min=min_element(c, c+5);
    int *it_max=max_element(c, c+5);
    cout<<*it_min<<"\t"<<*it_max<<endl;
    return 0;
    
}

7.词典比较

①lexicographical_compare

原型:template<class InIt1, class InIt2>

     bool lexicographical_compare(InIt1 first1, InIt1 last1,InIt2 first2,InIt2 last2);

         template<class InIt1, class InIt2, class Pred>

     bool lexicographical_compare(InIt1 first1, InIt1 last1,InIt2 first2,InIt2 last2, Predpr);

第1个模板函数功能是:该算法比较两个序列中的对应元素e1和e2(分别来自序列1和序列2)。如果e1<e2,则算法立即返回,返回值为真;

如果e2<e1,则算法也立即返回,返回值为假;否则继续对下一对元素进行比较。如果已到达第1个序列的末尾,但没有到达第2个序列的末尾,则算法返

回,返回值为真;否则返回假。第2个函数与第1个函数功能相近,只不过用二元比较函数pr代替了operator<。


//字典比较lexicographical_compare
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int a[]={1,2,3};
    int b[]={1,2,2};
    bool bRet=lexicographical_compare(a,a+3,b,b+3);
    cout<<(bRet==1? "a[]<b[]":"a[]>b[]")<<endl;
    return 0;
}

对三个容器进行排序字典序比较

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool mycompare(vector<int>&v1,vector<int>&v2)
{
    return lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
}
int main()
{
    int a[]={1,2,3};
    int b[]={1,2,2};
    int c[]={1,2,3,1};
    vector<int>v[3]={{vector<int>(a,a+3)},{vector<int>(b,b+3)},{vector<int>(c,c+4)}};
    cout<<"原始3个向量:"<<endl;
    for (int i=0; i<3; i++)
    {
        copy(v[i].begin(), v[i].end(), ostream_iterator<int>(cout,"\t"));
        cout<<endl;
    }
    sort(v, v+3, mycompare);
    cout<<"排序后:"<<endl;
    for (int i=0; i<3; i++)
    {
        copy(v[i].begin(), v[i].end(), ostream_iterator<int>(cout,"\t"));
        cout<<endl;
    }
    return 0;
}

8.排列生成器

①next_permutation

原型:template <classBidIt>

          boolnext_permutation(BidIt first,BidIt last);

          template <classBidIt, class Pred>

          boolnext_permutation(BidIt first, BidIt last,Pred pr)

第1个模板函数功能是:按operator<生成[first, last)指向容器的下一个词典序排列。第2个函数与第1个函数功能相近,只不过用二元比较函数pr代替了

operator<。

②prev_permutation

原型:template <classBidIt>

          boolprev_permutation(BidIt first,BidIt last);

          template <classBidIt, class Pred>

          boolprev_permutation(BidIt first, BidIt last,Pred pr)

第1个模板函数功能是:按operator<生成[first, last)指向容器的上一个词典序排列。第2个函数与第1个函数功能相近,只不过用二元比较函数pr代替了

operator<。

//排列生成器简单示例
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int a[]={2,3,4,5,6,1};
    const int N=sizeof(a)/sizeof(int);
    copy(a, a+N,ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    prev_permutation(a, a+N);
    cout<<"prev_permutation后:"<<endl;
    copy(a, a+N, ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    
    next_permutation(a, a+N);
    cout<<"next_permutation后:"<<endl;
    copy(a, a+N, ostream_iterator<int>(cout,"\t"));
    cout<<endl;
    return 0;
}

全排列

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int a[] = {0,1,2,3,4,5};
    sort(a, a+6);
    do
    {
        copy(a, a+6, ostream_iterator<int>(cout, "\t"));
        cout << endl;
    }while(next_permutation(a, a+6));
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值