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

  1. <tfoot id='Cy2Rq'></tfoot>

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

      <bdo id='Cy2Rq'></bdo><ul id='Cy2Rq'></ul>
    1. <i id='Cy2Rq'><tr id='Cy2Rq'><dt id='Cy2Rq'><q id='Cy2Rq'><span id='Cy2Rq'><b id='Cy2Rq'><form id='Cy2Rq'><ins id='Cy2Rq'></ins><ul id='Cy2Rq'></ul><sub id='Cy2Rq'></sub></form><legend id='Cy2Rq'></legend><bdo id='Cy2Rq'><pre id='Cy2Rq'><center id='Cy2Rq'></center></pre></bdo></b><th id='Cy2Rq'></th></span></q></dt></tr></i><div id='Cy2Rq'><tfoot id='Cy2Rq'></tfoot><dl id='Cy2Rq'><fieldset id='Cy2Rq'></fieldset></dl></div>
    2. 从 c++ std::vector 中删除所有项目

      时间:2023-09-15
      • <i id='33gPN'><tr id='33gPN'><dt id='33gPN'><q id='33gPN'><span id='33gPN'><b id='33gPN'><form id='33gPN'><ins id='33gPN'></ins><ul id='33gPN'></ul><sub id='33gPN'></sub></form><legend id='33gPN'></legend><bdo id='33gPN'><pre id='33gPN'><center id='33gPN'></center></pre></bdo></b><th id='33gPN'></th></span></q></dt></tr></i><div id='33gPN'><tfoot id='33gPN'></tfoot><dl id='33gPN'><fieldset id='33gPN'></fieldset></dl></div>
      • <legend id='33gPN'><style id='33gPN'><dir id='33gPN'><q id='33gPN'></q></dir></style></legend>

        <tfoot id='33gPN'></tfoot>

          <bdo id='33gPN'></bdo><ul id='33gPN'></ul>
        • <small id='33gPN'></small><noframes id='33gPN'>

                  <tbody id='33gPN'></tbody>
              1. 本文介绍了从 c++ std::vector 中删除所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试使用以下代码从 std::vector 中删除所有内容

                I'm trying to delete everything from a std::vector by using the following code

                vector.erase( vector.begin(), vector.end() );
                

                但它不起作用.

                更新:不清除破坏向量持有的元素?我不想那样,因为我还在使用对象,我只想清空容器

                Update: Doesn't clear destruct the elements held by the vector? I don't want that, as I'm still using the objects, I just want to empty the container

                推荐答案

                我认为你应该使用 std::vector::clear:

                I think you should use std::vector::clear:

                vec.clear();
                

                <小时>

                不清除破坏元素由向量持有?

                Doesn't clear destruct the elements held by the vector?

                是的.它在返回内存之前调用向量中每个元素的析构函数.这取决于您在向量中存储的元素".在以下示例中,我将对象本身存储在向量中:

                Yes it does. It calls the destructor of every element in the vector before returning the memory. That depends on what "elements" you are storing in the vector. In the following example, I am storing the objects them selves inside the vector:

                class myclass
                {
                public:
                    ~myclass()
                    {
                
                    }
                ...
                };
                
                std::vector<myclass> myvector;
                ...
                myvector.clear(); // calling clear will do the following:
                // 1) invoke the deconstrutor for every myclass
                // 2) size == 0 (the vector contained the actual objects).
                

                例如,如果您想在不同容器之间共享对象,则可以存储指向它们的指针.在这种情况下,当调用 clear 时,只释放指针内存,不接触实际对象:

                If you want to share objects between different containers for example, you could store pointers to them. In this case, when clear is called, only pointers memory is released, the actual objects are not touched:

                std::vector<myclass*> myvector;
                ...
                myvector.clear(); // calling clear will do:
                // 1) ---------------
                // 2) size == 0 (the vector contained "pointers" not the actual objects).
                

                对于评论中的问题,我认为getVector()是这样定义的:

                For the question in the comment, I think getVector() is defined like this:

                std::vector<myclass> getVector();
                

                也许你想返回一个引用:

                Maybe you want to return a reference:

                // vector.getVector().clear() clears m_vector in this case
                std::vector<myclass>& getVector(); 
                

                这篇关于从 c++ std::vector 中删除所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:二维向量的迭代器 下一篇:为什么 std::vector::operator[] 比 std::vector::at() 快 5

                相关文章

                最新文章

                <legend id='08nNu'><style id='08nNu'><dir id='08nNu'><q id='08nNu'></q></dir></style></legend>
                • <bdo id='08nNu'></bdo><ul id='08nNu'></ul>

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

                  <small id='08nNu'></small><noframes id='08nNu'>

                  <tfoot id='08nNu'></tfoot>