描述
C++ 函數std::vector::get_allocator()返回與向量關聯的分配器。
聲明
以下是 std::vector::get_allocator() 函數形式 std::vector 頭文件的聲明。
C++98
allocator_type get_allocator() const;
C++11
allocator_type get_allocator() const noexcept;
參數
空
返回值
返回與向量關聯的分配器。
異常
此成員函數從不拋出異常。
時間複雜度
常數,即 O(1)
示例
下麵的例子展示了 std::vector::get_allocator() 函數的用法。
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
vector<int> v = {1, 2, 3, 4, 5};
int *p = NULL;
/* allocate memory to store 5 elements */
p = v.get_allocator().allocate(5);
for (int i = 0; i < 5; ++i)
p[i] = i + 1;
for (int i = 0; i < 5; ++i)
cout << p[i] << endl;
return 0;
}
讓我們編譯並運行上麵的程序,這將產生以下結果——
1 2 3 4 5
相關用法
- C++ Vector front()用法及代碼示例
- C++ Vector emplace()用法及代碼示例
- C++ Vector capacity()用法及代碼示例
- C++ Vector hypot()用法及代碼示例
- C++ Vector rend()用法及代碼示例
- C++ Vector push_back()用法及代碼示例
- C++ Vector vector()用法及代碼示例
- C++ Vector insert()用法及代碼示例
- C++ Vector swap()用法及代碼示例
- C++ Vector pop_back()用法及代碼示例
- C++ Vector begin()用法及代碼示例
- C++ Vector size()用法及代碼示例
- C++ Vector crbegin()用法及代碼示例
- C++ Vector erase()用法及代碼示例
- C++ Vector resize()用法及代碼示例
- C++ Vector data()用法及代碼示例
- C++ Vector back()用法及代碼示例
- C++ Vector at()用法及代碼示例
- C++ Vector emplace_back()用法及代碼示例
- C++ Vector shrink_to_fit()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Vector Library - get_allocator() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。