Original Post
How can I write the following loop elegantly using a lambda and an std algorithm? It's just putting b[n] in position b_to_a_index[n] in a.
[source lang="cpp"]for (size_t i = 0; i < b.size(); ++i)
a[ b_to_a_index ] = b;[/source]
The following works, but having to do pointer arithmetic is ugly. Is there some nicer way?
[source lang="cpp"]std::for_each( b.begin(), b.end(),
[&](const int& b_val) {
size_t index = &b_val - &b[0];
a[ b_to_a_index[index] ] = b_val;
}
);[/source]
[source lang="cpp"]for (size_t i = 0; i < b.size(); ++i)
a[ b_to_a_index ] = b;[/source]
The following works, but having to do pointer arithmetic is ugly. Is there some nicer way?
[source lang="cpp"]std::for_each( b.begin(), b.end(),
[&](const int& b_val) {
size_t index = &b_val - &b[0];
a[ b_to_a_index[index] ] = b_val;
}
);[/source]