-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
basic_string.h:逻辑问题 #140
Comments
对于字符串move函数定义如下 // 字符串移动(复制),允许复制的两个地址段有重复,具体操作是更具源地址和目的地址关系选择从前后复制还是从后向前复制
static char_type* move(char_type* dst, const char_type* src, size_t n)
{
char_type* r = dst;
if (dst < src)
{
for (; n != 0; --n, ++dst, ++src)
*dst = *src;
}
else if (src < dst)
{
dst += n;
src += n;
for (; n != 0; --n)
*--dst = *--src;
}
return r;
} 最后一个参数n的类型是size_t类型,这里使用count作为移动字符的数目应该没有问题 |
移动字符的数目就有问题,应该是先把pos到end()这一段往后移动count,再把[first, last)拷贝进来,所以移动数目是end()-pos,对应dst是r+count,src是r。 |
我看了一下,确实如此,如果move第三个参数传递count,可能会导致某些值被覆盖而不是被移动,这逻辑确实有问题,还是等作者回复吧 |
是不是应该先将first到last之间的元素拷贝下来,在移动,因为移动可能改变了first和last指向的元素位置或者basic_string重新分配了内存 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
char_traits::move(r + count, r, count)应改为char_traits::move(r + count, r, end() - pos)
char_traits::move(r + len, r, len)应改为char_traits::move(r + len, r, end() - pos)
The text was updated successfully, but these errors were encountered: