我正在尝试获取给定集合的所有元素的地址并将它们复制到 std::set。基本上,而不是

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

我想插入他们的地址。就像是:
std::set<std::add_pointer<T>> s1;
std::copy(reference_iterator(first1), reference_iterator(last1),
    std::inserter(s1, s1.begin()));

在这里,reference_iterator 将是一个返回其元素地址的迭代器,而不是元素,这与 boostindirect_iterator 的作用相反。
有没有标准的方法来做到这一点?非常感谢。

最佳答案

使用 std::transform 进行修改复制。

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

关于c++ - 使用 std::copy 复制所有元素的地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24331956/

10-14 10:53
查看更多