Skip to content
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中erase删除 [first, last) 的元素的问题 #135

Open
haolian123 opened this issue Jun 11, 2023 · 1 comment
Open

basic_string.h中erase删除 [first, last) 的元素的问题 #135

haolian123 opened this issue Jun 11, 2023 · 1 comment

Comments

@haolian123
Copy link

// 删除 [first, last) 的元素
template <class CharType, class CharTraits>
typename basic_string<CharType, CharTraits>::iterator
basic_string<CharType, CharTraits>::
erase(const_iterator first, const_iterator last)
{
if (first == begin() && last == end())
{
clear();
return end();
}
const size_type n = end() - last;
iterator r = const_cast(first);
char_traits::move(r, last, n);
size_ -= (last - first);
return r;
}
考虑last到last+n的长度若小于last-first,这个函数只是将[first, last)范围内的元素移动到[first]的位置,但是并没有释放last,到末尾范围内的内存空间,是否最造成内存泄露问题

@frederick-vs-ja
Copy link
Contributor

没有问题。通常来说很难只释放一段动态分配的内存的其中一部分, C/C++ 标准没有提供任何能保证这么做的工具。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants