初始化向量指针的双端队列

发布于 2024-10-06 21:17:06 字数 901 浏览 10 评论 0 原文

class MD { 
    MD();
    MD(const MD &obj);
    MD& operator=(const MD &obj);
  private:
    int Name;
    double sal;
};

typedef std::shared_ptr<MD> mDataPtr;
typedef std::vector<mDataPtr> mDataVecContr;
typedef std::shared_ptr<mDataVecContr> ptrMdataVecContr;

class MDataContainer{
  public:
    MDataContainer();
    MDataContainer(const MDataContainer &mDataCont);
    MDataContainer& operator=(const MDataContainer &mDataCont);

  private:
    mDataVecContr vecNode;
    std::deque<ptrMdataVectContr> mDataQueContr;
};

我的要求是在一个向量中存储 500 个 MD 类型的对象,然后我将指向这些向量的指针保存在双端队列中。这里的问题是如何在复制构造函数 MDataContainer(const MDataContainer &mDataCont) 中初始化此列表,并在重载赋值运算符 MDataContainer& 中对其进行赋值。运算符=(常量MDataContainer&mDataCont)。为了消除代码重复,我使用 Init 函数。请向我解释一种可以提供更好性能的方法。我已经在使用一些粗略的方法。我们可以有一些算法或其他一些库(增强我没有 gr8 的想法)可以用来解决此类问题。

class MD { 
    MD();
    MD(const MD &obj);
    MD& operator=(const MD &obj);
  private:
    int Name;
    double sal;
};

typedef std::shared_ptr<MD> mDataPtr;
typedef std::vector<mDataPtr> mDataVecContr;
typedef std::shared_ptr<mDataVecContr> ptrMdataVecContr;

class MDataContainer{
  public:
    MDataContainer();
    MDataContainer(const MDataContainer &mDataCont);
    MDataContainer& operator=(const MDataContainer &mDataCont);

  private:
    mDataVecContr vecNode;
    std::deque<ptrMdataVectContr> mDataQueContr;
};

My requirement is to store 500 object of type MD in a vector then I am keeping pointer to these vectors in deque. The problem here is how to initialize this list in copy contructor MDataContainer(const MDataContainer &mDataCont) and assign it in overloaded assignment operator MDataContainer& operator=(const MDataContainer &mDataCont). To get rid of code duplication I am using Init function. Pls explain me a method which can give better performance. Some rough way I am already using. Can we have some algo or some other library(boost I am not having gr8 idea) which can be used to solve this kind of problem.

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

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

发布评论

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

评论(1

国际总奸 2024-10-13 21:17:06

如果您想要深层复制,您可能需要重新考虑您的设计。如果您有一个双端队列,其中包含 10 个(任意选取的数字)共享指针,这些指针指向 500 个 MD 对象共享指针的向量,则复制​​构造函数将需要从堆(最少)进行 5021 次分配,这将是庞大的。您确定需要指向向量和 MD 对象的智能指针吗?如果不是,则可以使用 std::deque> 将分配数量减少到 11。 mDataQueContr;

如果您确实想要带有智能指针的深复制,则必须循环构造它们,因为构造shared_ptr的复制将是浅复制。我还没有研究过shared_ptr,但假设它们像auto_ptr一样工作,你想要这样的东西。我使用迭代器,因为它们比双端队列上的索引更快。

MDataContainer::MDataContainer(const MDataContainer &mDataCont) {
    // initialize the deque to default null shared pointers
    mDataQueContr.resize(mDataCont.mDataQueContr.size());
    // for each super smart pointer
    std::dequeue<ptrMdataVectContr>::iterator destsup = mDataQueContr.begin();
    std::dequeue<ptrMdataVectContr>::const_iterator srcsup;
    srcsup = mDataCont.mDataQueContr.begin();
    for( ; destsup != mDataQueContr.end(); ++destsup,++srcsup) {
        // assign it a new vector of the right size of null shared pointers
        *destsup = new mDataVecContr((*srcsup)->size());
        // for each sub smart pointer
        mDataVecContr::iterator destsub = (*destsup)->begin();
        mDataVecContr::const_iterator srcsub = (*srcsup)->begin();
        for( ; destsub != (*destsup)->end(); ++destsub,++srcsub)
            *destsub = new MD(**srcsub); //assign it a new MD copy
    }

You may want to reconsider your design if you want a deep copy. If you have a deque containing 10(arbitrarily picked number) shared pointers to vectors of 500 shared pointers of MD objects, a copy constructor is going to require 5021 allocations from the heap, minimum, which is going to be bulky. Are you sure you need smart pointers to the vectors, and to the MD objects? If not, the number of allocations could be brought down to 11 with std::deque<std::vector<MD>> mDataQueContr;

If you do want the deep copy with smart pointers, you'll have to loop through to construct them, as copy constructing a shared_ptr will be a shallow copy. I haven't looked into shared_ptr yet, but assuming they work like auto_ptr, you want something like this. I use iterators, because they're faster than indexing on deque.

MDataContainer::MDataContainer(const MDataContainer &mDataCont) {
    // initialize the deque to default null shared pointers
    mDataQueContr.resize(mDataCont.mDataQueContr.size());
    // for each super smart pointer
    std::dequeue<ptrMdataVectContr>::iterator destsup = mDataQueContr.begin();
    std::dequeue<ptrMdataVectContr>::const_iterator srcsup;
    srcsup = mDataCont.mDataQueContr.begin();
    for( ; destsup != mDataQueContr.end(); ++destsup,++srcsup) {
        // assign it a new vector of the right size of null shared pointers
        *destsup = new mDataVecContr((*srcsup)->size());
        // for each sub smart pointer
        mDataVecContr::iterator destsub = (*destsup)->begin();
        mDataVecContr::const_iterator srcsub = (*srcsup)->begin();
        for( ; destsub != (*destsup)->end(); ++destsub,++srcsub)
            *destsub = new MD(**srcsub); //assign it a new MD copy
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文