zoukankan      html  css  js  c++  java
  • set_multiset_functor

    #include<iostream>
    #include<string>
    #include<set>
    using namespace std;
    
    class Student
    {
    public:
        Student(int age,string name)
        {
            m_age=age;
            m_name=name;
        }
    
        Student(const Student &stu)
        {
            m_age=stu.m_age;
            m_name=stu.m_name;
            cout << "Student "<<m_name<<","<<m_age<<endl;
        }
    
        int m_age;
        string m_name;
    };
    
    class stuFunctor{
    public:
        bool operator()(const Student &stu1,const Student &stu2)
        {
            return stu1.m_age>stu2.m_age;
        }
    };
    
    void TestStudent()
    {
        
        set<Student,stuFunctor> setStudent;
        //Student A(1,"aa"),B(2,"bb"),C(3,"cc");
        //setStudent.insert(A);
        //setStudent.insert(B);
        //setStudent.insert(C);
        setStudent.insert(Student(1,"aa"));
        setStudent.insert(Student(2,"bb"));
        setStudent.insert(Student(3,"cc"));
        
        set<Student,stuFunctor>::iterator it;
        for (it=setStudent.begin();it!=setStudent.end();++it)
        {
            cout<<it->m_name<<" ";
        }
        cout<<endl;
    /*
    Student aa,1
    Student bb,2
    Student cc,3
    cc bb aa
    请按任意键继续. . .
    */
    }
    
    
    void TestFunctor()
    {
        int a[6]={1,2,3,4,5,6};
        //set<int,less<int>> setInt;
        set<int,greater<int>> setInt;
        for (int i=0;i<6;i++)
        {
            setInt.insert(a[i]);
            setInt.insert(a[i]);
        }
        //set<int,less<int>>::iterator it;
        set<int,greater<int>>::iterator it;
        for (it=setInt.begin();it!=setInt.end();++it)
        {
            cout<<*it<<" ";
        }
        cout<<endl;
    }
    
    void TestMultiSet()
    {
        int a[6]={1,2,3,4,5,6};
        multiset<int> setInt;
        for (int i=0;i<6;i++)
        {
            setInt.insert(a[i]);
            setInt.insert(a[i]);
        }
        multiset<int>::iterator it;
        for (it=setInt.begin();it!=setInt.end();++it)
        {
            cout<<*it<<" ";
        }
        cout<<endl;
    /*
    1 1 2 2 3 3 4 4 5 5 6 6
    请按任意键继续. . .
    */
    }
    
    void main()
    {
        TestStudent();return;
        TestFunctor();return;
        TestMultiSet();return;
        int a[6]={1,2,3,4,5,6};
        set<int> setInt;
        for (int i=0;i<6;i++)
        {
            setInt.insert(a[i]);
            setInt.insert(a[i]);
        }
        set<int>::iterator it;
        for (it=setInt.begin();it!=setInt.end();++it)
        {
            cout<<*it<<" ";
        }
        cout<<endl;
    
    /*
    1 2 3 4 5 6
    请按任意键继续. . .
    */
      
    }
  • 相关阅读:
    表达式计算 六月飞雪
    code::blocks 单步执行 六月飞雪
    5.1 字符串 六月飞雪
    对使用倒序的一维数组解决0/1背包问题的理解 六月飞雪
    5.2 高精度运算 六月飞雪
    关于ArcEngine“不能再打开其他表了”的错误 (20121026 15:43:33)
    关于AO插入对象
    多线程使用实例
    C#程序运行时间长出现无法响应状态
    Geographic coordinate system和projected coordinate
  • 原文地址:https://www.cnblogs.com/sky20080101/p/6431562.html
Copyright © 2011-2022 走看看