vector resize的时候调用类的构造函数和析构函数问题

清水大湿 2015-04-15 03:25:06
代码很简单,如下:
#include <iostream>
#include <vector>

using namespace std;

class TestA
{
public:
TestA()
{
cout << "Create TestA" << endl;
}
~TestA()
{
cout << "Destory TestA" << endl;
}
};

int main()
{
vector<TestA> mytest;
mytest.resize(4);
mytest.resize(3);
return 0;
}

测试环境:vs2008,输出如下:
Create TestA
Destory TestA
Destory TestA
Create TestA
Destory TestA
Destory TestA
Destory TestA
Destory TestA
Destory TestA
Destory TestA
请按任意键继续. . .


http://www.cplusplus.com/reference/vector/vector/resize/ 对resize的解释如下:

Change size
Resizes the container so that it contains n elements.

If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).

If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.

If n is also greater than the current container capacity, an automatic reallocation of the allocated storage space takes place.

Notice that this function changes the actual content of the container by inserting or erasing elements from it.

我mytest.resize(4); n既大于当前容器大小又大于当前容量,create一次,为什么destory两次?
还有总得destory次数,为什么会有8次?总是比我两次resize的数字之和大1,求大神解答
...全文
582 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
清水大湿 2015-04-15
  • 打赏
  • 举报
回复
引用 5 楼 yangyunzhao 的回复:
[quote=引用 4 楼 yangyunzhao 的回复:] 再增加几行代码,更能说明问题:

#include <iostream>
#include <vector>

using namespace std;

class TestA
{
public:
	TestA()
	{
		cout << "Create TestA1" << endl;
	}
	TestA(const TestA & a)
	{
		cout << "Create TestA2" << endl;
	}
	~TestA()
	{
		cout << "Destory TestA" << endl;
	}
};

int main()
{
	vector<TestA> mytest;
	printf("%d\n", mytest.capacity());
	mytest.resize(4);
	printf("%d\n", mytest.capacity());
	mytest.resize(3);
	printf("%d\n", mytest.capacity());
	return 0;
}
执行结果:

0
Create TestA1
Create TestA2
Create TestA2
Create TestA2
Create TestA2
Destory TestA
4
Create TestA1
Destory TestA
Destory TestA
4
Destory TestA
Destory TestA
Destory TestA
请按任意键继续. . .

说错了,应该这样才对

int main()
{
	vector<TestA> mytest;
	printf("%d %d\n", mytest.capacity(), mytest.size());
	mytest.resize(4);
	printf("%d %d\n", mytest.capacity(), mytest.size());
	mytest.resize(3);
	printf("%d %d\n", mytest.capacity(), mytest.size());
	return 0;
}
[/quote] soga,只有第一次是调用默认构造函数,然后后面都是调用拷贝构造函数,谢谢
yangyunzhao 2015-04-15
  • 打赏
  • 举报
回复
引用 4 楼 yangyunzhao 的回复:
再增加几行代码,更能说明问题:

#include <iostream>
#include <vector>

using namespace std;

class TestA
{
public:
	TestA()
	{
		cout << "Create TestA1" << endl;
	}
	TestA(const TestA & a)
	{
		cout << "Create TestA2" << endl;
	}
	~TestA()
	{
		cout << "Destory TestA" << endl;
	}
};

int main()
{
	vector<TestA> mytest;
	printf("%d\n", mytest.capacity());
	mytest.resize(4);
	printf("%d\n", mytest.capacity());
	mytest.resize(3);
	printf("%d\n", mytest.capacity());
	return 0;
}
执行结果:

0
Create TestA1
Create TestA2
Create TestA2
Create TestA2
Create TestA2
Destory TestA
4
Create TestA1
Destory TestA
Destory TestA
4
Destory TestA
Destory TestA
Destory TestA
请按任意键继续. . .

说错了,应该这样才对

int main()
{
	vector<TestA> mytest;
	printf("%d %d\n", mytest.capacity(), mytest.size());
	mytest.resize(4);
	printf("%d %d\n", mytest.capacity(), mytest.size());
	mytest.resize(3);
	printf("%d %d\n", mytest.capacity(), mytest.size());
	return 0;
}
yangyunzhao 2015-04-15
  • 打赏
  • 举报
回复
再增加几行代码,更能说明问题:

#include <iostream>
#include <vector>

using namespace std;

class TestA
{
public:
	TestA()
	{
		cout << "Create TestA1" << endl;
	}
	TestA(const TestA & a)
	{
		cout << "Create TestA2" << endl;
	}
	~TestA()
	{
		cout << "Destory TestA" << endl;
	}
};

int main()
{
	vector<TestA> mytest;
	printf("%d\n", mytest.capacity());
	mytest.resize(4);
	printf("%d\n", mytest.capacity());
	mytest.resize(3);
	printf("%d\n", mytest.capacity());
	return 0;
}
执行结果:

0
Create TestA1
Create TestA2
Create TestA2
Create TestA2
Create TestA2
Destory TestA
4
Create TestA1
Destory TestA
Destory TestA
4
Destory TestA
Destory TestA
Destory TestA
请按任意键继续. . .

yangyunzhao 2015-04-15
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>

using namespace std;

class TestA
{
public:
	TestA()
	{
		cout << "Create TestA1" << endl;
	}
	TestA(const TestA & a)
	{
		cout << "Create TestA2" << endl;
	}
	~TestA()
	{
		cout << "Destory TestA" << endl;
	}
};

int main()
{
	vector<TestA> mytest;
	mytest.resize(4);
	mytest.resize(3);
	return 0;
}
赵4老师 2015-04-15
  • 打赏
  • 举报
回复
理解讨论之前请先学会如何观察! 计算机组成原理→DOS命令→汇编语言→C语言(不包括C++)、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言(包括C++)、架构…… 对学习编程者的忠告: 多用小脑和手,少用大脑、眼睛和嘴,会更快地学会编程! 眼过千遍不如手过一遍! 书看千行不如手敲一行! 手敲千行不如单步一行! 单步源代码千行不如单步Debug版对应汇编一行! 单步Debug版对应汇编千行不如单步Release版对应汇编一行! 单步类的实例“构造”或“复制”或“作为函数参数”或“作为函数返回值返回”或“参加各种运算”或“退出作用域”的语句对应的汇编代码几步后,就会来到该类的“构造函数”或“复制构造函数”或“运算符重载”或“析构函数”对应的C/C++源代码处。 VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。 (Turbo C或Borland C用Turbo Debugger调试,Linux或Unix下用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
  • 打赏
  • 举报
回复
vs2012测试没发现楼主说的情况: #include <iostream> using namespace std; #include <vector> class TestA { public: TestA(){ printf("create\n"); } ~TestA(){ printf("destroy\n"); } TestA( const TestA& test ){printf("copy\n");} }; int main(){ vector< TestA > test; test.resize(4); test.resize(3); system("pause"); return 0; } create create create create destroy 请按任意键继续. . . destroy destroy destroy 请按任意键继续. . .

64,681

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧