1. uninitialized_fill_n(ForwardIter first, Size n, const T& value)
从 first 位置开始,填充 n 个元素值,返回填充结束的位置
template <class ForwardIter, class Size, class T>
ForwardIter uninitialized_fill_n(ForwardIter first, Size n, const T& value)
{
return mystl::unchecked_uninit_fill_n(first, n, value,
std::is_trivially_copy_assignable<
typename iterator_traits<ForwardIter>::
value_type>{});
}
->
template <class ForwardIter, class Size, class T>
ForwardIter
unchecked_uninit_fill_n(ForwardIter first, Size n, const T& value, std::true_type)
{
return mystl::fill_n(first, n, value);
}
template <class ForwardIter, class Size, class T>
ForwardIter
unchecked_uninit_fill_n(ForwardIter first, Size n, const T& value, std::false_type)
{
auto cur = first;
try
{
for (; n > 0; --n, ++cur)
{
mystl::construct(&*cur, value);
}
}
catch (...)
{
for (; first != cur; ++first)
mystl::destroy(&*first);
}
return cur;
}
1.1 fill_n
template <class OutputIter, class Size, class T>
OutputIter fill_n(OutputIter first, Size n, const T& value)
{
return unchecked_fill_n(first, n, value);
}
->
template <class OutputIter, class Size, class T>
OutputIter unchecked_fill_n(OutputIter first, Size n, const T& value)
{
for (; n > 0; --n, ++first)
{
*first = value;
}
return first;
}
template <class Tp, class Size, class Up>
typename std::enable_if<
std::is_integral<Tp>::value && sizeof(Tp) == 1 &&
!std::is_same<Tp, bool>::value &&
std::is_integral<Up>::value && sizeof(Up) == 1,
Tp*>::type
unchecked_fill_n(Tp* first, Size n, Up value)
{
if (n > 0)
{
std::memset(first, (unsigned char)value, (size_t)(n));
}
return first + n;
}
|