Can UnaryOperator be a member function when std::transform is called

后端 未结 3 1002
终归单人心
终归单人心 2021-01-22 21:07

Based on std::transform

template < class InputIterator, class OutputIterator, class UnaryOperator >
  OutputIterator transform ( InputIterator first1, Inpu         


        
3条回答
  •  执笔经年
    2021-01-22 21:58

    It is pretty easy to do if you wrap the call in lambda:

    #include 
    #include 
    
    class C {
    public:
      int op() const { return 1; }
    };
    
    class D {
      int op() { return 1; }
      void f() {
        std::vector xs;
        std::vector ys;
        std::transform(xs.begin(), xs.end(), ys.begin(),
          [](const C& x) { return x.op(); });
        std::transform(xs.begin(), xs.end(), ys.begin(),
          [this](const C& x) { return this->op(); });
      }
    };
    

提交回复
热议问题