Skip to content

Commit 9f5bbf5

Browse files
committed
Improve __assign_with_sentinel in std::vector
1 parent 7ad63c0 commit 9f5bbf5

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
@@ -1031,9 +1031,14 @@ template <class _Tp, class _Allocator>
10311031
template <class _Iterator, class _Sentinel>
10321032
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
10331033
vector<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __first, _Sentinel __last) {
1034-
clear();
1035-
for (; __first != __last; ++__first)
1036-
emplace_back(*__first);
1034+
pointer __cur = __begin_;
1035+
for (; __first != __last && __cur != __end_; ++__cur, ++__first)
1036+
*__cur = *__first;
1037+
if (__cur != __end_)
1038+
__destruct_at_end(__cur);
1039+
else
1040+
for (; __first != __last; ++__first)
1041+
emplace_back(*__first);
10371042
}
10381043

10391044
template <class _Tp, class _Allocator>

0 commit comments

Comments
 (0)