posts - 43,comments - 3,trackbacks - 0
template< class T1, class T2 >
void construct(T1* p, const T2& value)
在C++中new 操作符首先分配object的内存,然后调用构造函数在该内存位置上构造object。可以分开如下:
int* pI = (int *)::operator new(sizeof (int));
::new (pI) int(100);
其中算法uninitialized_copy 、 uninitialized_file 、 unintialized_fill_n 和算法copy 、fill、fill_n的区别就在于此,前者们属于低阶算法,他们在给定的address处进行值copy

C++类似的操作符还有delete


有些算法,如stable_sort和inplace_merge,是所谓的"adaptive".他们会尝试分配额外的临时空间来放置中介结果.如果成功,则具有较佳运行时期复杂度。get_temporary_buffer 和return_temporary_buffer可以用来分配和释放未初始化的内存。至于get_temporary_buffer应该使用malloc或operator new,并没有任何规定。这些函数应该只用于内存真正是临时性的情况下,如果一个函数以get_temporary_buffer分配内存,则在该函数返回前,必得调用return_temporary_buffer来归还内存。
template<class _Ty> inline
    pair<_Ty *, _PDFT>
        get_temporary_buffer(_PDFT _Count)
    {    // get raw temporary buffer of up to _Count elements
    _Ty  *_Pbuf;

    if (_Count <= 0)
        _Count = 0;
    else if (((size_t)(-1) / _Count) < sizeof (_Ty))
        _THROW_NCEE(std::bad_alloc, NULL);

    for (_Pbuf = 0; 0 < _Count; _Count /= 2)
        if ((_Pbuf = (_Ty _FARQ *)operator new(
            (_SIZT)_Count * sizeof (_Ty), nothrow)) != 0)
            break;

    return (pair<_Ty _FARQ *, _PDFT>(_Pbuf, _Count));
    }


template<class _Ty> inline
    void return_temporary_buffer(_Ty *_Pbuf)
    {    // delete raw temporary buffer
    operator delete(_Pbuf);
    }

posted on 2008-02-08 13:01 RUI 阅读(834) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理