C++ 在c+;中使用向量时,如何调用构造函数和析构函数+;

C++ 在c+;中使用向量时,如何调用构造函数和析构函数+;,c++,vector,constructor,destructor,C++,Vector,Constructor,Destructor,我正在尝试下面的一个简单代码 #include <iostream> #include <string> #include <vector> using namespace std; class employee { private: int emp_id; public: void getEmpid(){cout<<emp_id<<endl;} void setEmpid

我正在尝试下面的一个简单代码

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class employee
{
    private:
        int emp_id;
    public:
        void getEmpid(){cout<<emp_id<<endl;}
        void setEmpid(){ cin>>emp_id;}
        employee():emp_id(10){cout<<"construct 1 "<<"employee id "<<emp_id<< endl;}
        employee(int empid):emp_id(empid){cout<<"construct 2 "<<"employee id "<<emp_id<<endl;}
        employee(const employee &emp):emp_id(emp.emp_id){cout<<"copy construct 3  "<<"employee id "<<emp_id<<endl;}
        employee(employee&& other) : emp_id(other.emp_id) {cout<<"move construct 4 "<<"employee id "<<emp_id<<endl;}
        ~employee(){cout<<"destructor"<<endl;}
};

int main()
{
    vector<employee>a;
    employee s[8]={1,2,3,4,5}; 

    for(int i=0;i<sizeof(s)/sizeof(s[0]);i++)
        a.push_back(s[i]);
    a.push_back(20);
    a.push_back(30);
    a.push_back(40);
    a.push_back(50);
    a.push_back(60);
    for(int i=0;i<a.size();i++)
        a[i].getEmpid();

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
班级员工
{
私人:
国际环境署;
公众:

void getEmpid(){cout对代码稍作修改将使输出易于理解。具体来说,更改析构函数:

   ~employee(){cout<<"destructor " << emp_id <<endl;} // shows emp_id when destructing
我得到的结果如下,我将对其进行拆分并解释:

关于向量的一些定义:

  • 容量:当前分配的存储器中可容纳的元素数
  • 大小:向量中的元素数
代码:员工s[3]={1,2};

输出:

creating array of employee[3]...

construct 2 employee id 1
construct 2 employee id 2
construct 1 employee id 10
    for(int i=0;i<sizeof(s)/sizeof(s[0]);i++) {
        cout << "Vec size now: " << a.size() << " Capacity: " << a.capacity() << endl;
        a.push_back(s[i]);
    }
starting loop, copy into vec a

//iteration 1
Vec size now: 0 Capacity: 0 //vec is zero initially
copy construct 3  employee id 1 //id 1 is copy constructed and pushed back

//iteration 2
Vec size now: 1  Capacity: 1  //vec is now size: 1, with 1 element
//we are doing a push back, but the size needs to grow. Vector reallocates and size becomes 2. The previous memory and the elements in that memory are deallocated/destructed.

copy construct 3  employee id 2 //push back(), copy contruct into vector
copy construct 3  employee id 1 // since we reallocated, the previous memory is gone,
                                //we need to add the previous element as well
destructor 1                    //previous element, id: 1, now being destructed.

//iteration 3
//follows the same logic as in iteration 2. size by the end will be 3 (3 elements). Capacity will be 4. That means we can do one more push back without destroying everything and reallocating.

Vec size now: 2  Capacity: 2
copy construct 3  employee id 10
copy construct 3  employee id 1
copy construct 3  employee id 2
destructor 1
destructor 2

Outside, Vec size now: 3 Capacity: 4                                                                                                    
    a.push_back(20);
    a.push_back(30);
doing push back outside loop
//remember we have capacity 4

construct 2 employee id 20     //construct id 20
move construct 4 employee id 20 //move into push_back() (because 20 is temporary, rvalue)
destructor 20                   //destroy what we constructed

//capacity: 4, size: 4

construct 2 employee id 30      // construct id 30
move construct 4 employee id 30 //move into push_back()
//uh oh.. capacity was full, deallocations, destructions, reconstructions:
copy construct 3  employee id 1
copy construct 3  employee id 2
copy construct 3  employee id 10
copy construct 3  employee id 20
destructor 1
destructor 2
destructor 10
destructor 20
destructor 30 //destroy our temporary id: 30
调用三个构造函数,2
employee(int)
和1个default。数组的前两个元素调用
employee(int)
。第三个元素是默认构造的

代码:

creating array of employee[3]...

construct 2 employee id 1
construct 2 employee id 2
construct 1 employee id 10
    for(int i=0;i<sizeof(s)/sizeof(s[0]);i++) {
        cout << "Vec size now: " << a.size() << " Capacity: " << a.capacity() << endl;
        a.push_back(s[i]);
    }
starting loop, copy into vec a

//iteration 1
Vec size now: 0 Capacity: 0 //vec is zero initially
copy construct 3  employee id 1 //id 1 is copy constructed and pushed back

//iteration 2
Vec size now: 1  Capacity: 1  //vec is now size: 1, with 1 element
//we are doing a push back, but the size needs to grow. Vector reallocates and size becomes 2. The previous memory and the elements in that memory are deallocated/destructed.

copy construct 3  employee id 2 //push back(), copy contruct into vector
copy construct 3  employee id 1 // since we reallocated, the previous memory is gone,
                                //we need to add the previous element as well
destructor 1                    //previous element, id: 1, now being destructed.

//iteration 3
//follows the same logic as in iteration 2. size by the end will be 3 (3 elements). Capacity will be 4. That means we can do one more push back without destroying everything and reallocating.

Vec size now: 2  Capacity: 2
copy construct 3  employee id 10
copy construct 3  employee id 1
copy construct 3  employee id 2
destructor 1
destructor 2

Outside, Vec size now: 3 Capacity: 4                                                                                                    
    a.push_back(20);
    a.push_back(30);
doing push back outside loop
//remember we have capacity 4

construct 2 employee id 20     //construct id 20
move construct 4 employee id 20 //move into push_back() (because 20 is temporary, rvalue)
destructor 20                   //destroy what we constructed

//capacity: 4, size: 4

construct 2 employee id 30      // construct id 30
move construct 4 employee id 30 //move into push_back()
//uh oh.. capacity was full, deallocations, destructions, reconstructions:
copy construct 3  employee id 1
copy construct 3  employee id 2
copy construct 3  employee id 10
copy construct 3  employee id 20
destructor 1
destructor 2
destructor 10
destructor 20
destructor 30 //destroy our temporary id: 30
代码:

creating array of employee[3]...

construct 2 employee id 1
construct 2 employee id 2
construct 1 employee id 10
    for(int i=0;i<sizeof(s)/sizeof(s[0]);i++) {
        cout << "Vec size now: " << a.size() << " Capacity: " << a.capacity() << endl;
        a.push_back(s[i]);
    }
starting loop, copy into vec a

//iteration 1
Vec size now: 0 Capacity: 0 //vec is zero initially
copy construct 3  employee id 1 //id 1 is copy constructed and pushed back

//iteration 2
Vec size now: 1  Capacity: 1  //vec is now size: 1, with 1 element
//we are doing a push back, but the size needs to grow. Vector reallocates and size becomes 2. The previous memory and the elements in that memory are deallocated/destructed.

copy construct 3  employee id 2 //push back(), copy contruct into vector
copy construct 3  employee id 1 // since we reallocated, the previous memory is gone,
                                //we need to add the previous element as well
destructor 1                    //previous element, id: 1, now being destructed.

//iteration 3
//follows the same logic as in iteration 2. size by the end will be 3 (3 elements). Capacity will be 4. That means we can do one more push back without destroying everything and reallocating.

Vec size now: 2  Capacity: 2
copy construct 3  employee id 10
copy construct 3  employee id 1
copy construct 3  employee id 2
destructor 1
destructor 2

Outside, Vec size now: 3 Capacity: 4                                                                                                    
    a.push_back(20);
    a.push_back(30);
doing push back outside loop
//remember we have capacity 4

construct 2 employee id 20     //construct id 20
move construct 4 employee id 20 //move into push_back() (because 20 is temporary, rvalue)
destructor 20                   //destroy what we constructed

//capacity: 4, size: 4

construct 2 employee id 30      // construct id 30
move construct 4 employee id 30 //move into push_back()
//uh oh.. capacity was full, deallocations, destructions, reconstructions:
copy construct 3  employee id 1
copy construct 3  employee id 2
copy construct 3  employee id 10
copy construct 3  employee id 20
destructor 1
destructor 2
destructor 10
destructor 20
destructor 30 //destroy our temporary id: 30
输出:

creating array of employee[3]...

construct 2 employee id 1
construct 2 employee id 2
construct 1 employee id 10
    for(int i=0;i<sizeof(s)/sizeof(s[0]);i++) {
        cout << "Vec size now: " << a.size() << " Capacity: " << a.capacity() << endl;
        a.push_back(s[i]);
    }
starting loop, copy into vec a

//iteration 1
Vec size now: 0 Capacity: 0 //vec is zero initially
copy construct 3  employee id 1 //id 1 is copy constructed and pushed back

//iteration 2
Vec size now: 1  Capacity: 1  //vec is now size: 1, with 1 element
//we are doing a push back, but the size needs to grow. Vector reallocates and size becomes 2. The previous memory and the elements in that memory are deallocated/destructed.

copy construct 3  employee id 2 //push back(), copy contruct into vector
copy construct 3  employee id 1 // since we reallocated, the previous memory is gone,
                                //we need to add the previous element as well
destructor 1                    //previous element, id: 1, now being destructed.

//iteration 3
//follows the same logic as in iteration 2. size by the end will be 3 (3 elements). Capacity will be 4. That means we can do one more push back without destroying everything and reallocating.

Vec size now: 2  Capacity: 2
copy construct 3  employee id 10
copy construct 3  employee id 1
copy construct 3  employee id 2
destructor 1
destructor 2

Outside, Vec size now: 3 Capacity: 4                                                                                                    
    a.push_back(20);
    a.push_back(30);
doing push back outside loop
//remember we have capacity 4

construct 2 employee id 20     //construct id 20
move construct 4 employee id 20 //move into push_back() (because 20 is temporary, rvalue)
destructor 20                   //destroy what we constructed

//capacity: 4, size: 4

construct 2 employee id 30      // construct id 30
move construct 4 employee id 30 //move into push_back()
//uh oh.. capacity was full, deallocations, destructions, reconstructions:
copy construct 3  employee id 1
copy construct 3  employee id 2
copy construct 3  employee id 10
copy construct 3  employee id 20
destructor 1
destructor 2
destructor 10
destructor 20
destructor 30 //destroy our temporary id: 30
全部完成,现在将逐个调用所有元素的析构函数

All done exiting...

destructor 10
destructor 2
destructor 1
destructor 1
destructor 2
destructor 10
destructor 20
destructor 30
教训:尽可能地使用。 此外,仔细观察,看看它的作用。阅读文档了解更多信息

  • 关于病媒增长的链接:

欢迎使用StackOverflow!如果您在每个函数中打印出员工ID,可能会有助于解释发生了什么。不太清楚构造函数和析构函数是如何调用的,以及调用顺序--您应该减少
main
程序,以便得到一个更易于管理的示例。您的事情太多了在
main
中打开。例如,与
vector
无关的第一行,
employee s[8]={1,2,3,4,5};
生成输出。你为什么不自己尝试一下呢?使用调试器,在涉及类对象的每个语句上设置断点,查看输出屏幕,观察变量。提出你的理论部分,如果你错了,我们会很乐意纠正你。确实尝试过,但不确定分配/解除分配n当容量已满时,感谢Waqar的回答非常感谢@Waqar,不确定容量已满时的分配/解除分配。