• STL之容器、迭代器、算法、仿函数


    1、仿函数

        系统已有的:
        greater
        less
        
        系统已有,但比较特殊:
        可以调用自己的成员函数    mem_fun_ref-------参数是: &成员函数名   如:,mem_fun_ref(&CStudent::print_student)
                                  注意:print_student函数必须要有返回值----是语法这么要求的
                                  好处:直接可以使用 成员函数了,而不用自定义 回调函数
                                  经常和 for_each算法 搭配使用
        
        自定义的:
        对象函数-----   注意:好记----赋值函数   第5大成员函数
        
        
    容器------数组

    利用迭代器、算法 来实现 数组 的 排序、查找
    #include
    #include
    #include
    #include
    #include
    #include "Student.h"
    using namespace std;

    int main()
    {
        int array1[]={4,2,7,5,8,1};
        //sort(array1,array1+6,less());
        int *p2=find(array1,array1+6,8);   //注意:find函数的第2个参数,要求是 最后1个元素的下一个的地址,因此加了6
        if(p2!=array1+6)                    //如果p2走到了array1+6,则说明没有找到
        {
            cout<<"找到了:"<<(*p2)<     }
        for(int *p=array1;p!=array1+6;p++)    //这是采用迭代器法 遍历元素的思想写的代码;我们原来不这么用!
        {
            cout<<(*p)<<"\t";
        }
        cout<     

        return 0;

        
        
    一个常用的仿函数:----mem_fun_ref

    #include
    #include
    #include
    #include
    #include
    #include "Student.h"
    using namespace std;


    void print(CStudent &other)
    {
        other.print_student();
        return;
    }


    int main()
    {
        CStudent stu1(1003,"zhangsan");
        CStudent stu2(1001,"lisi");
        CStudent stu3(1002,"wangwu");
        vector vec1;
        vec1.push_back(stu1);
        vec1.push_back(stu2);
        vec1.push_back(stu3);

        //for_each(vec1.begin(),vec1.end(),print);
        for_each(vec1.begin(),vec1.end(),mem_fun_ref(&CStudent::print_student));
        
        return 0;

  • 相关阅读:
    Qwt-QwtPlot类详解
    (已解决)执行bat脚本文件的@title 设置中文标题后执行,出现中文乱码
    第二章Redis概述安装
    JavaScript常用事件详解
    PEP最新提案推出,Python将上线重大更新,带来了哪些新功能?
    线程池工作原理及参数
    光谱数据处理:1.特征波长优选的不同方法与Python实现
    leetcode刷题日记:94. Binary Tree Inorder Traversal(二叉树的中序遍历)
    操作系统和计算机网络连环问,你能坚持到第几问?
    事务隔离(二)
  • 原文地址:https://blog.csdn.net/zzjlhlcd/article/details/127711994