Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy the address of all elements using std::copy

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.

like image 679
LeCoc Avatar asked Dec 26 '22 07:12

LeCoc


1 Answers

Use std::transform to copy with modification.

std::transform(first1, last1, std::inserter(s1, s1.begin()),
               std::addressof<T>);
like image 85
Brian Bi Avatar answered Dec 29 '22 02:12

Brian Bi