• C++ STL function object学习


    C++ STL函数对象

    cfunctionobject2020101101.cpp

    //cfunctionobject2020101101.cpp
    #include <iostream>
    #include <list>
    #include <algorithm>
    #include <iterator>
    
    #include "print.hpp"
    
    using namespace std;
    
    class IntSequence
    {
    private:
        int value;
    public:
        IntSequence(int initialValue):value(initialValue) 
        {}
    
        //
        int operator() ()
        {
            return ++value;
        }
    };
    
    
    
    int main()
    {
        list<int> coll;
    
        //insert values from 1 to 9
        generate_n(back_inserter(coll),
            9,
            IntSequence(1));
        
        PRINT_ELEMENTS(coll);
    
        //replace seconds to last element but one with values starting at 42
        generate(next(coll.begin()),
            prev(coll.end()),
            IntSequence(42));
    
        PRINT_ELEMENTS(coll);
    }

    print.hpp

    //print.hpp
    #include <iostream>
    #include <string>
    
    template <typename T>
    inline void PRINT_ELEMENTS(const T& coll, const std::string& optstr = "")
    {
        std::cout << optstr;
        for (const auto& elem : coll)
        {
            std::cout << elem << "  ";
        }
        std::cout << std::endl;
    }

    运行结果

    2 3 4 5 6 7 8 9 10
    2 43 44 45 46 47 48 49 10

  • 相关阅读:
    HDU
    Hdu 5072 Coprime(容斥+同色三角形)
    HDU
    HTML常用基础标签
    简单session实现
    前端中的 IoC 理念
    怎样做页面界限
    Reset 对象属性
    SQL注入
    js:表单校验(获取元素、事件)
  • 原文地址:https://www.cnblogs.com/herd/p/13799264.html
Copyright © 2020-2023  润新知