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

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

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

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

        对象指针向量上的 vector::erase() 是否会破坏对象本

        时间:2023-09-16
          <tbody id='9VFZ4'></tbody>
      1. <i id='9VFZ4'><tr id='9VFZ4'><dt id='9VFZ4'><q id='9VFZ4'><span id='9VFZ4'><b id='9VFZ4'><form id='9VFZ4'><ins id='9VFZ4'></ins><ul id='9VFZ4'></ul><sub id='9VFZ4'></sub></form><legend id='9VFZ4'></legend><bdo id='9VFZ4'><pre id='9VFZ4'><center id='9VFZ4'></center></pre></bdo></b><th id='9VFZ4'></th></span></q></dt></tr></i><div id='9VFZ4'><tfoot id='9VFZ4'></tfoot><dl id='9VFZ4'><fieldset id='9VFZ4'></fieldset></dl></div>

      2. <tfoot id='9VFZ4'></tfoot>

              <bdo id='9VFZ4'></bdo><ul id='9VFZ4'></ul>
              <legend id='9VFZ4'><style id='9VFZ4'><dir id='9VFZ4'><q id='9VFZ4'></q></dir></style></legend>

              1. <small id='9VFZ4'></small><noframes id='9VFZ4'>

                • 本文介绍了对象指针向量上的 vector::erase() 是否会破坏对象本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个指向对象的指针向量.我需要从向量中删除一个元素并将该元素放在另一个列表中.

                  I have a vector of pointers to objects. I need to remove an element from the vector and place that element in another list.

                  我读到擦除可用于从向量中删除对象,但我也读到它在这样做之前调用了对象析构函数.

                  I read that erase can be used to remove the object from the vector, but I also read that it calls the objects destructor before doing so.

                  我需要知道擦除对象是否也会破坏它.

                  I need to know whether or not erasing the object will destroy it as well.

                  推荐答案

                  vector::erase
                  从向量容器中移除并调用其析构函数,但如果包含的对象是一个指针,则它不会拥有销毁它的所有权.

                  vector::erase
                  Removes from the vector container and calls its destructor but If the contained object is a pointer it doesnt take ownership of destroying it.

                  您必须对每个包含的指针显式调用 delete 以删除它指向的内容,例如:

                  You will have to explicitly call delete on each contained pointer to delete the content it is pointing to, for example:

                  void clearVectorContents( std::vector <YourClass*> & a ) 
                  {    
                      for ( int i = 0; i < a.size(); i++ ) 
                      {       
                          delete a[i];    
                      }    
                      a.clear(); 
                  } 
                  

                  在标准容器中存储原始指针不是一个好主意.如果你真的需要存储必须由new分配的资源,那么你应该使用boost::shared_ptr.查看 Boost 文档.

                  Storing raw pointers in standard containers is not a good idea. If you really need to store resources that have to be allocated by new, then you should use boost::shared_ptr. Check out the Boost documentation.

                  更通用的 &优雅的解决方案:
                  此解决方案使用 for_each &templates 正如@Billy 在评论中指出的:

                  An more generic & elegant solution:
                  This solution makes use of for_each & templates as @Billy pointed out in comments:

                  // Functor for deleting pointers in vector.
                  template<class T> class DeleteVector
                  {
                      public:
                      // Overloaded () operator.
                      // This will be called by for_each() function.
                      bool operator()(T x) const
                      {
                          // Delete pointer.
                          delete x;
                          return true;
                      }
                  };
                  

                  这可以称为:

                  for_each( myclassVector.begin(),myclassVector.end(),
                            DeleteVector<myclass*>());
                  

                  其中,myclassVector 是包含指向 myclass 类对象的指针的向量.

                  where, myclassVector is your vector containing pointers to myclass class objects.

                  用法示例:

                  #include "functional"
                  #include "vector"
                  #include "algorithm"
                  #include "iostream"
                  
                  //Your class
                  class myclass
                  {
                      public:
                          int i;
                          myclass():i(10){}
                  };
                  
                  
                  // Functor for deleting pointers in vector.
                  template<class T> class DeleteVector
                  {
                      public:
                      // Overloaded () operator.
                      // This will be called by for_each() function.
                      bool operator()(T x) const
                      {
                          // Delete pointer.
                          delete x;
                          return true;
                      }
                  };
                  
                  
                  int main()
                  {
                      // Add 10 objects to the vector.
                      std::vector<myclass*> myclassVector;
                  
                      for( int Index = 0; Index < 10; ++Index )
                      {
                          myclassVector.push_back( new myclass);
                      }
                  
                      for (int i=0; i<myclassVector.size(); i++) 
                      {
                          std::cout << " " << (myclassVector[i])->i;
                      }
                  
                      // Now delete the vector contents in a single  line.
                      for_each( myclassVector.begin(),
                                myclassVector.end(),
                                DeleteVector<myclass*>());
                  
                      //Clear the vector 
                      myclassVector.clear();
                  
                      std::cout<<"
                  "<<myclassVector.size();
                  
                      return 0;
                  }
                  

                  这篇关于对象指针向量上的 vector::erase() 是否会破坏对象本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:连接两个向量的最佳方法是什么? 下一篇:找到多边形的质心?

                  相关文章

                  最新文章

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

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

                      <tfoot id='zHzTE'></tfoot>

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