C++/STL: std::transform with given stride?

后端 未结 4 798
既然无缘
既然无缘 2021-01-12 17:53

I have a 1d array containing Nd data, I would like to effectively traverse on it with std::transform or std::for_each.

unigned int nelems;
unsigned int strid         


        
4条回答
  •  不要未来只要你来
    2021-01-12 18:47

    Well, I have decided to use for_each instead of transform any other decisions are welcome:

    generator gen(0, 1);
                vector idx(m_nelem);//make an index
                std::generate(idx.begin(), idx.end(),gen);
                std::for_each(idx.begin(), idx.end(), strMover(&pPOS[0],&m_COM[0],stride));
    

    where

    template T op_sum (T i, T j) { return i+j; }
    template 
    class strMover
        {
        T *pP_;
        T *pMove_;
        unsigned int stride_;
        public:
            strMover(T *pP,T *pMove, unsigned int stride):pP_(pP), pMove_(pMove),stride_(stride)
                {}
            void operator() ( const unsigned int ip )
                {
                std::transform(&pP_[ip*stride_], &pP_[ip*stride_]+stride_, 
                    pMove_, &pP_[ip*stride_], op_sum);
                }
        };
    

    From first look this is a thread safe solution.

提交回复
热议问题