迭代 unique_ptr 的容器

发布于 2024-12-17 11:07:29 字数 276 浏览 6 评论 0 原文

如何在不夺走容器所有权的情况下访问容器的 unique_ptr 元素(通过迭代器)?当获得容器中某个元素的迭代器时,元素所有权是否仍属于容器?当人们取消引用迭代器以获得对 unique_ptr 的访问时怎么样?这是否执行 unique_ptr 的隐式移动?

我发现当我需要在容器中存储元素(而不是按值)时,我经常使用shared_ptr,即使容器概念上拥有这些元素,而其他代码只是希望操作容器中的元素,因为我担心在不夺取容器所有权的情况下,无法实际访问容器中的 unique_ptr 元素。

有什么见解吗?

How does one access unique_ptr elements of a container (via an iterator) without taking ownership away from the container? When one gets an iterator to an element in the container is the element ownership still with the container? How about when one dereferences the iterator to gain access to the unique_ptr? Does that perform an implicit move of the unique_ptr?

I find I'm using shared_ptr a lot when I need to store elements in a container (not by value), even if the container conceptually owns the elements and other code simply wishes to manipulate elements in the container, because I'm afraid of not being able to actually access the unique_ptr elements in the container without ownership being taken from it.

Any insights?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2024-12-24 11:07:29

使用 auto 和 C++11 基于范围的 for 循环,这变得相对优雅:

std::vector< std::unique_ptr< YourClass >> pointers;
for( auto&& pointer : pointers ) {
    pointer->functionOfYourClass();
}

std::unique_ptr 的引用 & > 避免复制,您可以使用 uniqe_ptr 而无需取消引用。

With auto and the range-based for-loops of C++11 this becomes relatively elegant:

std::vector< std::unique_ptr< YourClass >> pointers;
for( auto&& pointer : pointers ) {
    pointer->functionOfYourClass();
}

The reference & to the std::unique_ptr avoids the copying and you can use the uniqe_ptr without dereferencing.

半世蒼涼 2024-12-24 11:07:29

只要您不尝试复制 unique_ptr,就可以使用它。您必须“双重取消引用”迭代器才能获取指针的值,就像使用 shared_ptr 所做的那样。这是一个简短的示例:

#include <vector>
#include <memory>
#include <iostream>

template <class C>
void
display(const C& c)
{
    std::cout << '{';
    if (!c.empty())
        std::cout << *c.front();
    for (auto i = std::next(c.begin()); i != c.end(); ++i)
        std::cout << ", " << **i;
    std::cout << "}\n";
}

int main()
{
    typedef std::unique_ptr<int> Ptr;
    std::vector<Ptr> v;
    for (int i = 1; i <= 5; ++i)
        v.push_back(Ptr(new int(i)));
    display(v);
    for (auto i = v.begin(); i != v.end(); ++i)
        **i += 2;
    display(v);
}

如果您(不小心)制作了 unique_ptr 的副本:

Ptr p = v[0];

那么您将在编译时找到答案。它不会导致运行时错误。您的用例就是构建 container> 的原因。事情应该正常工作,如果不正常,问题会出现在编译时而不是运行时。因此,请继续编写代码,如果您不明白编译时错误,请回到此处提出另一个问题。

As long as you don't try to make a copy of the unique_ptr, you can just use it. You'll have to "double dereference" the iterator to get to the pointer's value, just as you would have to with shared_ptr. Here's a brief example:

#include <vector>
#include <memory>
#include <iostream>

template <class C>
void
display(const C& c)
{
    std::cout << '{';
    if (!c.empty())
        std::cout << *c.front();
    for (auto i = std::next(c.begin()); i != c.end(); ++i)
        std::cout << ", " << **i;
    std::cout << "}\n";
}

int main()
{
    typedef std::unique_ptr<int> Ptr;
    std::vector<Ptr> v;
    for (int i = 1; i <= 5; ++i)
        v.push_back(Ptr(new int(i)));
    display(v);
    for (auto i = v.begin(); i != v.end(); ++i)
        **i += 2;
    display(v);
}

If you do (accidentally) make a copy of the unique_ptr:

Ptr p = v[0];

then you'll find out at compile time. It won't cause a run time error. Your use case is why container<unique_ptr<T>> was built. Things should just work, and if they don't, the problem appears at compile time instead of run time. So code away, and if you don't understand the compile time error, then ask another question back here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文