ssercpp12 / Homework

Homework result for 12 sser cpp
1 stars 8 forks source link

About the Vector class #24

Open sysuyc opened 11 years ago

sysuyc commented 11 years ago

英语无力,还是用中文表达清楚点。 我的私有成员是一个T*二级指针,然后每次扩容是重新申请一个一级指针数组,然后一级指针再对应申请一块T对象的内存。我这样做的理由是,假设capacity = 50,size = 50时: ① 如果私有成员是一级指针,那么要 new T[100]; ② 如果私有成员是二级指针,那么是 new T[100]; 我想的是,如果用第一种方法,但是最后里面才用了五十一个元素的话,后面四十九个元素占用的内存就没什么用了。如果用第二种,最后是申请了五十一块对应的内存,而后面的四十九个元素只保存了一级指针。 然后就蛋疼地去查了一下指针的大小这类内容。在自己电脑上测试了一下,貌似不管是指向什么类型的指针,大小都是8个字节,int占用的是4字节。所以我感觉如果Vector储存的数据类型比较大的话(大于8字节),是否用第二种方法也是可行的?

但是写到一半就卡住了。因为作业定义的迭代器是用一级指针模拟的,而我定义的是用二级指针模拟的....

所以想知道我的那种办法是否可行,是否是有什么疏漏而不能用?

omegaga commented 11 years ago

quoted from definition of vector on cplusplus.com

vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays.

It seems that elements in your vector are not stored contiguously. Actually, your method looks more like a deque. A deque stores its elements in several blocks.

sysuyc commented 11 years ago

I use contiguous storage locations for the pointers of their elements. So I thought they were the same before.

omegaga commented 11 years ago

Therefore data itself is not contiguously stored, right?

On Wednesday, June 5, 2013, sysuyc wrote:

I use contiguous storage locations for the pointers of their elements.

— Reply to this email directly or view it on GitHubhttps://github.com/ssercpp12/Homework/issues/24#issuecomment-18923986 .

此致 李健弘 中山大学信息科学与技术学院 计算机科学与技术

sysuyc commented 11 years ago

That‘s right. =-=