C++/STL:带有给定步幅的std :: transform?

Arm*_*man 6 c++ foreach stl transform

我有一个包含Nd数据的1d数组,我想用std :: transform或std :: for_each有效地遍历它.

unigned int nelems;
unsigned int stride=3;// we are going to have 3D points
float *pP;// this will keep xyzxyzxyz...
Load(pP);
std::transform(pP, pP+nelems, strMover<float>(pP, stride));//How to define the strMover??
Run Code Online (Sandbox Code Playgroud)

Arm*_*man 1

好吧,我决定使用 for_each 而不是转换,欢迎任何其他决定:

generator<unsigned int> gen(0, 1);
            vector<unsigned int> idx(m_nelem);//make an index
            std::generate(idx.begin(), idx.end(),gen);
            std::for_each(idx.begin(), idx.end(), strMover<float>(&pPOS[0],&m_COM[0],stride));
Run Code Online (Sandbox Code Playgroud)

在哪里

template<class T> T op_sum (T i, T j) { return i+j; }
template<class T> 
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<T>);
            }
    };
Run Code Online (Sandbox Code Playgroud)

乍一看,这是一个线程安全的解决方案。