C++ forward_list 庫 - get_allocator() 函式



描述

C++ 函式std::forward_list::get_allocator() 返回與 forward_list 關聯的分配器。

宣告

以下是來自 std::forward_list 標頭檔案的 std::forward_list::get_allocator() 函式宣告。

C++11

allocator_type get_allocator() const noexcept;

引數

返回值

返回與 forward_list 關聯的分配器。

異常

此成員函式從不丟擲異常。

時間複雜度

常數,即 O(1)

示例

以下示例演示了 std::forward_list::get_allocator() 函式的使用。

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl = {1, 2, 3, 4, 5};
   int *p = NULL;

   p = fl.get_allocator().allocate(5);

   for (int i = 0; i < 5; ++i)
      p[i] = i + 1;

   cout << "List contains following elements" << endl;

   for (int i = 0; i < 5; ++i)
      cout << p[i] << endl;

   return 0;
}

讓我們編譯並執行上述程式,這將產生以下結果:

List contains following elements
1
2
3
4
5
forward_list.htm
廣告

© . All rights reserved.