STL源代码分析(ch2 内存分配)uninitialized_fill_n
生活随笔
收集整理的這篇文章主要介紹了
STL源代码分析(ch2 内存分配)uninitialized_fill_n
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. uninitialized_fill_n(ForwardIter first, Size n, const T& value)
從 first 位置開始,填充 n 個元素值,返回填充結(jié)束的位置
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
// 從 first 位置開始填充 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; }// 為 one-byte 類型提供特化版本 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; }總結(jié)
以上是生活随笔為你收集整理的STL源代码分析(ch2 内存分配)uninitialized_fill_n的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stdthread(7)并发unique
- 下一篇: 深度学习的数学 (6)误差反向传播法必需