Skip to content

Commit e2ec5a0

Browse files
committed
Improve __assign_with_sentinel in std::vector
1 parent 3deee23 commit e2ec5a0

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

libcxx/include/__vector/vector.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,9 +1017,14 @@ template <class _Tp, class _Allocator>
10171017
template <class _Iterator, class _Sentinel>
10181018
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
10191019
vector<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __first, _Sentinel __last) {
1020-
clear();
1021-
for (; __first != __last; ++__first)
1022-
emplace_back(*__first);
1020+
pointer __cur = __begin_;
1021+
for (; __first != __last && __cur != __end_; ++__cur, ++__first)
1022+
*__cur = *__first;
1023+
if (__cur != __end_)
1024+
__destruct_at_end(__cur);
1025+
else
1026+
for (; __first != __last; ++__first)
1027+
emplace_back(*__first);
10231028
}
10241029

10251030
template <class _Tp, class _Allocator>

0 commit comments

Comments
 (0)