错题-数据结构

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为非基本类型且有对应的赋值运算符以执行深复制时,前一段代码会调用赋值运算符,而后一段只能进行浅复制。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章