错题-数据结构

Is it possible to replace:
是否可以将视频里向量扩容代码中的:

for (int i = 0; i < _size; i++) _elem[i] = oldElem[i];

in the vector expansion code in the video with:
替代为:

memcpy(_elem, oldElem, _size * sizeof(T));

答案:
No, because whether the latter can achieve the purpose is related to element type T.
否,因为后者能否达到目的与元素类型T有关。

分析:
When T is a non-base type and there is a corresponding assignment operator to perform deep copy, the previous section of code calls the assignment operator, and the latter section can only perform shallow copy.

当T为非基本类型且有对应的赋值运算符以执行深复制时,前一段代码会调用赋值运算符,而后一段只能进行浅复制。

发布了43 篇原创文章 · 获赞 80 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43868654/article/details/101556833