简体   繁体   English

使用std :: copy复制所有元素的地址

[英]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. 我正在尝试获取给定集合的所有元素的地址,并将它们复制到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. 这里,reference_iterator将是一个迭代器,它返回其元素的地址而不是元素,这与boost indirect_iterator的作用相反。 Is there any standard way to do it? 有没有标准的方法呢? Many thanks. 非常感谢。

Use std::transform to copy with modification. 使用std::transform进行修改。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM