• <legend id='IUXbL'><style id='IUXbL'><dir id='IUXbL'><q id='IUXbL'></q></dir></style></legend>

      <small id='IUXbL'></small><noframes id='IUXbL'>

    1. <i id='IUXbL'><tr id='IUXbL'><dt id='IUXbL'><q id='IUXbL'><span id='IUXbL'><b id='IUXbL'><form id='IUXbL'><ins id='IUXbL'></ins><ul id='IUXbL'></ul><sub id='IUXbL'></sub></form><legend id='IUXbL'></legend><bdo id='IUXbL'><pre id='IUXbL'><center id='IUXbL'></center></pre></bdo></b><th id='IUXbL'></th></span></q></dt></tr></i><div id='IUXbL'><tfoot id='IUXbL'></tfoot><dl id='IUXbL'><fieldset id='IUXbL'></fieldset></dl></div>

        • <bdo id='IUXbL'></bdo><ul id='IUXbL'></ul>

        <tfoot id='IUXbL'></tfoot>

        std::vector 是否使用 push_back 复制对象?

        Is std::vector copying the objects with a push_back?(std::vector 是否使用 push_back 复制对象?)

        1. <small id='tgUeH'></small><noframes id='tgUeH'>

          <tfoot id='tgUeH'></tfoot>
        2. <legend id='tgUeH'><style id='tgUeH'><dir id='tgUeH'><q id='tgUeH'></q></dir></style></legend>
            • <bdo id='tgUeH'></bdo><ul id='tgUeH'></ul>

                <tbody id='tgUeH'></tbody>

                <i id='tgUeH'><tr id='tgUeH'><dt id='tgUeH'><q id='tgUeH'><span id='tgUeH'><b id='tgUeH'><form id='tgUeH'><ins id='tgUeH'></ins><ul id='tgUeH'></ul><sub id='tgUeH'></sub></form><legend id='tgUeH'></legend><bdo id='tgUeH'><pre id='tgUeH'><center id='tgUeH'></center></pre></bdo></b><th id='tgUeH'></th></span></q></dt></tr></i><div id='tgUeH'><tfoot id='tgUeH'></tfoot><dl id='tgUeH'><fieldset id='tgUeH'></fieldset></dl></div>

                • 本文介绍了std::vector 是否使用 push_back 复制对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在对 valgrind 进行大量调查后,我得出的结论是 std::vector 制作了一个您想要 push_back 的对象的副本.

                  After a lot of investigations with valgrind, I've made the conclusion that std::vector makes a copy of an object you want to push_back.

                  真的是这样吗?一个向量不能在没有副本的情况下保留对象的引用或指针?!

                  Is that really true ? A vector cannot keep a reference or a pointer of an object without a copy ?!

                  谢谢

                  推荐答案

                  是的,std::vector::push_back() 创建参数的副本并将其存储在向量中.如果您想在向量中存储指向对象的指针,请创建一个 std::vector 而不是 std::vector.

                  Yes, std::vector<T>::push_back() creates a copy of the argument and stores it in the vector. If you want to store pointers to objects in your vector, create a std::vector<whatever*> instead of std::vector<whatever>.

                  但是,您需要确保指针引用的对象在向量持有对它们的引用时保持有效(使用 RAII 惯用语的智能指针解决了问题).

                  However, you need to make sure that the objects referenced by the pointers remain valid while the vector holds a reference to them (smart pointers utilizing the RAII idiom solve the problem).

                  这篇关于std::vector 是否使用 push_back 复制对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                  • <bdo id='EofTq'></bdo><ul id='EofTq'></ul>

                    <legend id='EofTq'><style id='EofTq'><dir id='EofTq'><q id='EofTq'></q></dir></style></legend>

                      <i id='EofTq'><tr id='EofTq'><dt id='EofTq'><q id='EofTq'><span id='EofTq'><b id='EofTq'><form id='EofTq'><ins id='EofTq'></ins><ul id='EofTq'></ul><sub id='EofTq'></sub></form><legend id='EofTq'></legend><bdo id='EofTq'><pre id='EofTq'><center id='EofTq'></center></pre></bdo></b><th id='EofTq'></th></span></q></dt></tr></i><div id='EofTq'><tfoot id='EofTq'></tfoot><dl id='EofTq'><fieldset id='EofTq'></fieldset></dl></div>
                        • <small id='EofTq'></small><noframes id='EofTq'>

                            <tbody id='EofTq'></tbody>
                            <tfoot id='EofTq'></tfoot>