繁体   English   中英

匿名构造函数和析构函数调用向量中

[英]Anonymous constructor and destructor call in vector

对下面的程序及其输出有点困惑。

有人可以澄清一下吗?

#include <iostream>
#include <vector>

 using namespace std;
class sample
{
   public:
    int a;
    sample( ){ cout<<" Am in Default Cons "<< a << "\n"; }
    sample( int b ){ a=b; cout<<" Am in Cons "<< a << "\n"; }
    sample(const sample& s){ a=s.a; cout<<" Am in copy " <<s.a<<"\n"; }
    ~sample(){ cout<<" Am in Des "<<a <<"\n"; }

};

int main()
{
    vector < sample > Object;
    {
     Object.push_back(  sample( 1 ));
     Object.push_back(  sample( 2 ));
     cout<<" End of loop \n";
    }
    cout<<" \n End of Program \n";
    return 0; 
}

而输出是

Am in Cons 1 // Understood this is for object 1 creation                                                                                                                                         
 Am in copy 1 // Understood this is for object 1 copy to vector 
 Am in Des 1 // Understood this is for object 1 destruction.
 Am in Cons 2  // Understood this is for object 2 creation                                                                                                                                                 
 Am in copy 2 // Understood this is for object 2 copy to vector                                                                                                                                              
 Am in copy 1 // **Don't understand this COPY CALL**                                                                                                                                              
 Am in Des 1  // **Don't understand this DESTRUCTOR CALL**                                                                                                                                               
 Am in Des 2 // Understood this is for object 2 destruction.                                                                                                                                            
 End of loop                                                                                                                                               
 End of Program                                                                                                                                            
 Am in Des 1 //// Understood this is for object 1 destruction in vector .                                                                                                                                         
 Am in Des 2 //// Understood this is for object 2 destruction in vector .

std::vector是一个动态数组。 添加元素并且其内部缓冲区的容量已满时, vector需要:

  1. 分配新的内存缓冲区,
  2. 将元素从旧缓冲区复制/移动到新缓冲区,
  3. 破坏原始缓冲区中的元素,
  4. 解除分配原始缓冲区,
  5. 在新缓冲区的末尾复制/移动添加的元素。

(不一定按此顺序。)

第2步涉及您不理解的复制构造函数。 第3步涉及你不理解的析构函数。


要防止这种(低效)行为,请在插入元素之前使用std::vector::reserve (如果可以)。 然后,不会重新分配,也不会发生隐藏的复制/移动元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM