诡异的vector

快乐田伯光 2011-08-28 01:59:59
程序如下:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;
class test
{
friend ostream &operator << (ostream &output, const test &obj)
{
output << obj.value << endl;
}
public:
test(int a) : value(a) { cout << "construct " << value << endl;};
~test() {cout << "destruct " << value << endl;};
private:
int value;
};


int main(int argc, char *argv[])
{
test t1(1),t2(2), t3(3), t4(4);
ostream_iterator<test> output(cout, " ");

vector<test> vt;
vt.push_back(t1);
vt.push_back(t2);
vt.push_back(t3);
vt.push_back(t4);
copy(vt.begin(), vt.end(), output1);
cout << "erase first elemant: " << endl;
vt.erase(vt.begin());
copy(vt.begin(), vt.end(), output);
}


输出如下:

:!./a.out
construct 1
construct 2
construct 3
construct 4
destruct 1
destruct 1
destruct 2
1
2
3
4
erase first elemant:
destruct 4
2
3
4

我现在对红色标识的destruct 4这么一条输出表示不理解
...全文
186 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
nehckjykcaj 2011-08-28
  • 打赏
  • 举报
回复
别给马甲分,全给大号就行了

快乐田伯光 2011-08-28
  • 打赏
  • 举报
回复
呵呵,我们理解完全一样。
[Quote=引用 11 楼 nehckjykcaj 的回复:]

3次回复,正用马甲呢

而前面vc和gcc之所以不同,在于vector的实现机制,push_back是要延长内存的,连续内存的话就需要类似于placement new(realloc)这样的机制,会造成原有位置的对象全部释放。

gcc的优化比较靠前,延长2次之后,会分配一个比较大的空间;而VC的至少在你的这个数量级上,没有这种优化,每次push_back都伴随一次realloc。

……
[/Quote]
nehckjykcaj 2011-08-28
  • 打赏
  • 举报
回复
3次回复,正用马甲呢

而前面vc和gcc之所以不同,在于vector的实现机制,push_back是要延长内存的,连续内存的话就需要类似于placement new(realloc)这样的机制,会造成原有位置的对象全部释放。

gcc的优化比较靠前,延长2次之后,会分配一个比较大的空间;而VC的至少在你的这个数量级上,没有这种优化,每次push_back都伴随一次realloc。

而我又增加了两个元素,t5,t6,从t6开始,VC也不realloc了
快乐田伯光 2011-08-28
  • 打赏
  • 举报
回复
呵呵,我们理解一样。至于你的输出跟我的输出不一样,应该是对push_back的动作需要添加新的内存块的大小选择不一样,你那边选择了多分配一个元素的空间,而我这边的选择是分配当前元素的的两倍。
[Quote=引用 9 楼 jackyjkchen 的回复:]

我的理解是,vector虽然删除的是第一个位置,但vector作为连续内存,所有后面的往前移,真正释放掉的是最后一个位置的对象
[/Quote]
jackyjkchen 2011-08-28
  • 打赏
  • 举报
回复
我的理解是,vector虽然删除的是第一个位置,但vector作为连续内存,所有后面的往前移,真正释放掉的是最后一个位置的对象
jackyjkchen 2011-08-28
  • 打赏
  • 举报
回复
先贴VS2010的结果,和你不太一样


construct 1
construct 2
construct 3
construct 4
destruct 1
destruct 1
destruct 2
destruct 1
destruct 2
destruct 3
1 2 3 4
erase first elemant:
destruct 4
2 3 4
destruct 2
destruct 3
destruct 4
destruct 4
destruct 3
destruct 2
destruct 1

jackyjkchen 2011-08-28
  • 打赏
  • 举报
回复
还是有,那个友元没返回值,VS2010不过

不过不影响逻辑,我看下
快乐田伯光 2011-08-28
  • 打赏
  • 举报
回复

源代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;
class test
{
friend ostream &operator << (ostream &output, const test &obj)
{
output << obj.value;
}
public:
test(int a) : value(a) { cout << "construct " << value << endl;};
~test() {cout << "destruct " << value << endl;};
private:
int value;
};


int main(int argc, char *argv[])
{
test t1(1),t2(2), t3(3), t4(4);
ostream_iterator<test> output(cout, " ");

vector<test> vt;
vt.push_back(t1);
vt.push_back(t2);
vt.push_back(t3);
vt.push_back(t4);
copy(vt.begin(), vt.end(), output);
cout << endl;
cout << "erase first elemant: " << endl;
vt.erase(vt.begin());
copy(vt.begin(), vt.end(), output);
cout << endl;

return 0;
}

输出:
:!g++ b.cpp

:!./a.out
construct 1
construct 2
construct 3
construct 4
destruct 1
destruct 1
destruct 2
1 2 3 4
erase first elemant:
destruct 4
2 3 4
destruct 2
destruct 3
destruct 4
destruct 4
destruct 3
destruct 2
destruct 1


这份代码没有笔误
快乐田伯光 2011-08-28
  • 打赏
  • 举报
回复
你试着解释一下,呵呵,看看我们理解的是不是一样
[Quote=引用 4 楼 jackyjkchen 的回复:]

全散我吧
[/Quote]
jackyjkchen 2011-08-28
  • 打赏
  • 举报
回复
全散我吧
快乐田伯光 2011-08-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 guosha 的回复:]
程序如下:
C/C++ code

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;
class test
{
friend ostream &operator << (ostream &output, const te……
[/Quote]

那是笔误,就是output, 没有output1
快乐田伯光 2011-08-28
  • 打赏
  • 举报
回复
哦,理解了,改散分吧。
jackyjkchen 2011-08-28
  • 打赏
  • 举报
回复
output1是什么

64,660

社区成员

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

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