首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::get_temporary_buffer

Defined in header <memory>

?

?

template< class T > std::pair< T*, std::ptrdiff_t > get_temporary_buffer( std::ptrdiff_t count );

?

(deprecated in C++17)

分配未初始化的连续存储,该存储应足以存储到count相邻类型对象T请求是不具有约束力的,实现可以分配更少或更多的存储所需的资源。count相邻的物体。

参数

count

-

the desired number of objects

返回值

std::pair保存指向分配存储开始的指针和实际分配的存储中适合的对象数。

如果无法分配内存,或分配的存储空间不足以存储单个类型的元素。Tfirst元素为空指针,second元素是零。

例外

(none)

(until C++11)

noexcept specification: noexcept

(since C++11)

二次

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <iterator>
 
int main()
{
    const std::string s[] = {"string", "1", "test", "..."};
    const auto p = std::get_temporary_buffer<std::string>(4);
    // requires that p.first is passed to return_temporary_buffer
    // (beware of early exit points and exceptions)
 
    std::copy(s, s + p.second,
              std::raw_storage_iterator<std::string*, std::string>(p.first));
    // requires that each string in p is individually destroyed
    // (beware of early exit points and exceptions)
 
    std::copy(p.first, p.first + p.second,
              std::ostream_iterator<std::string>{std::cout, "\n"});
 
    std::for_each(p.first, p.first + p.second, [](std::string& e) {
        e.~basic_string<char>();
    });
 
    std::return_temporary_buffer(p.first);
}

二次

产出:

二次

代码语言:javascript
复制
string
1
test
...

二次

另见

return_temporary_buffer (deprecated in C++17)

frees uninitialized storage (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com