C++在 boost::ptr_container 中共享元素?

发布于 2024-11-16 03:17:07 字数 1217 浏览 2 评论 0 原文

请考虑以下代码:

int main()
{
    typedef boost::ptr_vector<int> ptr_vector;

    ptr_vector vec0;
    vec0.push_back(new int(1));
    vec0.push_back(new int(2));
    vec0.push_back(new int(3));
    vec0.push_back(new int(4));
    vec0.push_back(new int(5));

    ptr_vector::iterator last  = boost::prior(vec0.end()),
                         first = boost::prior(last, 3);

    ptr_vector vec1(first, last); // this will copy '2, 3, 4' to vec1

    struct print
    {
        void operator()(int const& i) {
            std::cout << i.m_i << std::endl;
        }
    };

    std::for_each(vec0.begin(), vec0.end(), print());   // 1, 2, 3, 4, 5
    std::for_each(vec1.begin(), vec1.end(), print());   // 2, 3, 4

    return 0;
}

我不想将元素复制vec1中,而是以shared_ptr<>的方式共享> 提供。我的要求基本上是:

  • 在另一个容器实例中共享一系列对象的相同实例,这些对象是容器的一部分
  • 不想在这两个以上的容器中共享一个实例
  • 希望在一个元素“突然”出现时收到通知" 从其他容器中删除(或者至少检查一下,例如 shared_ptr::unique()

两个容器都是同一类的一部分。因此,它们具有相同的范围,并且会同时被破坏。这些类的构造函数构造两个容器。建造完成后,这些容器不会进行任何修改。

我是否需要使用 shared_ptr<>std::vector 或者还有其他解决方案吗?

Please consider the following piece of code:

int main()
{
    typedef boost::ptr_vector<int> ptr_vector;

    ptr_vector vec0;
    vec0.push_back(new int(1));
    vec0.push_back(new int(2));
    vec0.push_back(new int(3));
    vec0.push_back(new int(4));
    vec0.push_back(new int(5));

    ptr_vector::iterator last  = boost::prior(vec0.end()),
                         first = boost::prior(last, 3);

    ptr_vector vec1(first, last); // this will copy '2, 3, 4' to vec1

    struct print
    {
        void operator()(int const& i) {
            std::cout << i.m_i << std::endl;
        }
    };

    std::for_each(vec0.begin(), vec0.end(), print());   // 1, 2, 3, 4, 5
    std::for_each(vec1.begin(), vec1.end(), print());   // 2, 3, 4

    return 0;
}

I don't want to copy the elements into vec1, but sharing in a way that shared_ptr<> provides. My requirements basically are:

  • Sharing the same instances of a range of objects, which are part of a container, in another container instance
  • Don't want to share one instance in more than these two containers
  • Want to be notified when one element is "suddenly" erased from the other container (or at least get check this, e.g. something like shared_ptr::unique())

Both containers are part of the same class. So, they have the same scope and will be destroyed at the same time. The constructor of these class constructs both containers. After construction there won't be any modification to these containers.

Do I need to use a std::vector<> of shared_ptr<> instead or is there any other solution?

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

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

发布评论

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

评论(1

吖咩 2024-11-23 03:17:07

是的,您应该使用 vector>

由于您只处理范围,因此您可以组合一个自定义解决方案来跟踪范围及其交集(用于实现unique)。然后,您可以将所有内容存储在向量中并拥有范围索引到那个。这可能会更快(只是因为避免了缓存未命中),但实施起来会需要更多工作。

Yes, you should use a vector<shared_ptr<int>>.

Since you're only working with ranges, you could put together a custom solution that keeps track of the ranges and their intersections (for implementing unique.) Then, you could store everything in vector and have the ranges index into that. This would probably be faster (just on account of the avoided cache misses), but it would be more work to implement.

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