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

后端 未结 4 760
既然无缘
既然无缘 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:38

    The answer is not to change strMover, but to change your iterator. Define a new iterator class which wraps a float * but moves forward 3 places when operator++ is called.

    You can use boost's Permutation Iterator and use a nonstrict permutation which only includes the range you are interested in.

    If you try to roll your own iterator, there are some gotchas: to remain strict to the standard, you need to think carefully about what the correct "end" iterator for such a stride iterator is, since the naive implementation will merrily stride over and beyond the allowed "one-past-the-end" to the murky area far past the end of the array which pointers should never enter, for fear of nasal demons.

    But I have to ask: why are you storing an array of 3d points as an array of floats in the first place? Just define a Point3D datatype and create an array of that instead. Much simpler.

提交回复
热议问题