#std::for_each index

12 messages · Page 1 of 1 (latest)

hallow iris
#

Is there an easy, standard method of acquiring the index of the current element from within the function/lambda? Something like:

std::for_each(std::par_unseq, vec.begin(), vec.end(), [](int element, size_t index)
{});

Additionally, are there any similar functions which allow doing a similar thing? I need the par_unseq. Something where the function parameter accepts an index rather than an element would be ideal.

pure pelicanBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

frank yew
#

std::for_ech is mostly a legacy construct from before range-based for loops were a thing

hallow iris
#

range based for loops don't have the execution policy though, right? so it would require manual parallelization

frank yew
#

yeah

hallow iris
#

hmm, so i basically have to do this as there's nothing in the stl

frank yew
#

you could probably make an iterator that returns itself when dereferenced, so you get the original iterator in the call that for_each makes

#

and then you can use the iterator difference to get the index

#

or if it's for a contiguous range like a std::vector, you can get the index by getting the pointer difference from the first element

hallow iris
#

hmm ok thanks

#

!solved