数据结构 - 如何理解C++区间删除算法中的“更新规模”操作?
怪我咯
怪我咯 2017-04-17 13:34:29
0
4
681

正在自学数据结构,遇到一个问题(如图)始终难以理解:_size=lo为更新规模,或者说丢弃尾部的操作,为什么不是hi=_size,因为元素移位后,有效的数据序列应该是n-hi+lo位置之前的元素构成的,也就是vi[hi]之后的元素都应该丢弃,所以hi=_size不是很合理吗。
希望大神可以帮忙解决,谢谢!

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(4)
黄舟

The line where you put the arrow has accumulated the variable lo

伊谢尔伦

Because the elements between [hi,_size] are used to cover the elements in the [lo, _size+lo-hi] interval.
hi increases to _size, and the corresponding lo will increase to _size+lo- hi,
that is, _size+lo-hi is the position of the last element, that is, the new _size value

The code uses lo to mark the position of the last element, so the final size is _size=lo.

刘奇

In this function, lo and hi are both value-passed parameters, that is, these two values ​​​​are destroyed after the function is completed.
is a member of vector. It uses _size to determine the number of elements saved. _sizeSo what needs to be modified here is
, not _size. hi

黄舟

hi=_sizeUnreasonable

    The meaning of
  • hi is the right endpoint of the interval to be deleted at the beginning, and after while ends, it represents the right endpoint of the remaining elements; before _size is updated, it represents the right endpoint of the element before it is deleted, whileAfter the end, the unupdated _size is meaningless. What the hell is assigning hi to _size

  • lo and hi are local variables (parameters), and their scope is within the function. They are destroyed after the function is executed. It is meaningless to update hi.

  • represents the size of the vector _size, which is the value returned by calling v.size(), so _size

  • should be updated
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template