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

          <bdo id='VuWFH'></bdo><ul id='VuWFH'></ul>

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

      1. std::引用向量

        时间:2023-09-15
              <tbody id='GX7bC'></tbody>
            • <bdo id='GX7bC'></bdo><ul id='GX7bC'></ul>
              • <small id='GX7bC'></small><noframes id='GX7bC'>

                <legend id='GX7bC'><style id='GX7bC'><dir id='GX7bC'><q id='GX7bC'></q></dir></style></legend>
                  <tfoot id='GX7bC'></tfoot>

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

                  本文介绍了std::引用向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有这样的问题:我有 Foo 类,如果有这个类的一些对象,

                  I have such problem: I have class Foo, and if have some objects of this class,

                  Foo a();
                  

                  我需要把这个对象放到 2 个不同的向量中:

                  I need to put this object to 2 different vectors:

                  std::vector<Foo> vA, vB;
                  

                  如果avA中发生变化,它应该在vB、向量vA中发生变化vB 可以不同,但​​它们可以具有相同的对象.我知道可以使用 Boost,但我不能使用 Boost.

                  and if a changes in vA it should be changed in vB, vectors vA and vB can be different, but they can have same objects. I know that it is possible to do with Boost, but I can't use Boost.

                  推荐答案

                  有一些可能性:

                  1. 存储一个指针向量(如果您的向量共享指针的所有权,则使用):

                  std::vector<std::shared_ptr<Foo>> vA, vB;
                  

                2. 存储包装引用的向量(如果向量不共享指针的所有权,并且您知道引用的对象在生命周期后有效,则使用向量):

                3. Store a vector of wrapped references (use if the vectors do not share ownership of the pointers, and you know the object referenced are valid past the lifetime of the vectors):

                  std::vector<std::reference_wrapper<Foo>> vA, vB;
                  

                4. 存储原始指针向量(如果您的向量不共享指针的所有权,和/或存储的指针可能会因其他因素而变化,请使用):

                5. Store a vector of raw pointers (use if your vectors do not share ownership of the pointers, and/or the pointers stored may change depending on other factors):

                  std::vector<Foo*> vA, vB;
                  

                  这对于观察、跟踪分配等很常见.原始指针的常见警告适用:不要在对象生命周期结束后使用指针访问对象.

                  This is common for observation, keeping track of allocations, etc. The usual caveats for raw pointers apply: Do not use the pointers to access the objects after the end of their life time.

                  存储包装对象的 std::unique_ptr 向量(如果您的向量想要移交指针的所有权,在这种情况下生命周期为被引用对象的数量由 std::unique_ptr 类的规则控制):

                  Store a vector of std::unique_ptr that wrap the objects (use if your vectors want to handover the ownership of the pointers in which case the lifetime of the referenced objects are governed by the rules of std::unique_ptr class):

                  std::vector<std::unique_ptr<Foo>> vA, vB;
                  

                6. 这篇关于std::引用向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何逐个添加两个 STL 向量的元素? 下一篇:如何在C++中将文件读入向量?

                  相关文章

                  最新文章

                7. <legend id='fGshb'><style id='fGshb'><dir id='fGshb'><q id='fGshb'></q></dir></style></legend>

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

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

                        <bdo id='fGshb'></bdo><ul id='fGshb'></ul>
                    1. <tfoot id='fGshb'></tfoot>