Copy the address of all elements using std :: copy

I am trying to get the address of all the elements in this collection and copy them to std :: set. In principle, instead of

std::set<T> s1;
std::copy(first1, last1, std::inserter(s1, s1.begin()));

I would like to insert their addresses. Sort of:

std::set<std::add_pointer<T>> s1;
std::copy(reference_iterator(first1), reference_iterator(last1), 
    std::inserter(s1, s1.begin()));

Here, reference_iterator will be an iterator that returns the address of its element, not the element, something opposite to what boost_iterator boost does. Is there a standard way to do this? Many thanks.

+4
source share
1 answer

Use std::transformto copy with changes.

std::transform(first1, last1, std::inserter(s1, s1.begin()),
               std::addressof<T>);
+10
source

Source: https://habr.com/ru/post/1545405/


All Articles