STL vector中的get_allocator方法(27)

public member function
<vector>

std::vector::get_allocator

allocator_type get_allocator() const noexcept;
Get allocator
Returns a copy of the allocator object associated with the vector.
返回vector的内存分配器。
//这个有人说我们初学者基本不会用到,我觉得也的确是,因此,现阶段我们就只是了解好了。

Parameters

none

Return Value

The allocator.
返回值是一个内存分配器对象。

Member type allocator_type is the type of the allocator used by the container, defined in vector as an alias of its second template parameter (Alloc).
其类型由vector模版参数Alloc指定。

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// vector::get_allocator
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;
  int * p;
  unsigned int i;

  // allocate an array with space for 5 elements using vector's allocator:
  p = myvector.get_allocator().allocate(5);

  // construct values in-place on the array:
  for (i=0; i<5; i++) myvector.get_allocator().construct(&p[i],i);

  std::cout << "The allocated array contains:";
  for (i=0; i<5; i++) std::cout << ' ' << p[i];
  std::cout << '\n';

  // destroy and deallocate:
  for (i=0; i<5; i++) myvector.get_allocator().destroy(&p[i]);
  myvector.get_allocator().deallocate(p,5);

  return 0;
}

The example shows an elaborate way to allocate memory for an array of ints using the same allocator used by the vector. Output:
The allocated array contains: 0 1 2 3 4
这个例子详细说明了array分配内存的过程。
运行截图:

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed. 容器将被访问。
No contained elements are accessed: concurrently accessing or modifying them is safe.
容器内的元素不会被访问。同时访问以及修改他们都是安全的。

Exception safety

No-throw guarantee: this member function never throws exceptions. 该方法不会i抛出异常。
Copying any instantiation of the default allocator is also guaranteed to never throw.
复制任何实例化的默认构造器也不会抛出异常。

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

2014-8-17

于GDUT





  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值