I'm trying to obtain the address of all elements of a given collection and copy them to a std::set. Basically, instead of
std::set<T> s1;
std::copy(first1, last1, std::inserter(s1, s1.begin()));
I'd like to insert their addresses. Something like:
std::set<std::add_pointer<T>> s1;
std::copy(reference_iterator(first1), reference_iterator(last1),
std::inserter(s1, s1.begin()));
Here, reference_iterator would be an iterator returning the address of its element, instead of the element, something opposed to what the boost indirect_iterator does. Is there any standard way to do it? Many thanks.
Use std::transform
to copy with modification.
std::transform(first1, last1, std::inserter(s1, s1.begin()),
std::addressof<T>);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With