![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ List::get_allocator() Function
The C++ std::list::get_allocator() function is used to retrieve an allocator associated with the list.
In C++, allocators are used in the standard library to handle the allocation and deallocation of elements stored in containers. Allocation is a process of requesting access to a data set. If you allocate a data set that exists, the system allows you to open the data set, and if you allocate a data set that does not exist, the system creates space for it on an available device and allows you to open that space.
Syntax
Following is the syntax of the C++ std::list::get_allocator() −
allocator_type get_allocator() const;
Parameters
- It does not accept any parameter.
Return Value
This function returns an allocator associated with list.
Example 1
In the following program, we are using the C++ std::list::get_allocator() function to retrieve an allocator that is associated with the current list {10, 20, 30, 40, 50}.
#include<iostream> #include<list> using namespace std; int main(void) { //create a list list<int> num_list = {10, 20, 30, 40, 50}; cout<<"List elements are: "; for(int i : num_list){ cout<<i<<" "; } int *p = NULL; p = num_list.get_allocator().allocate(5); for (int i = 0; i<5; ++i) p[i] = i + 1; cout<<"\nList contains following elements"<<endl; for (int i = 0; i<5; ++i) cout<<p[i]<<" "; return 0; }
Output
Following is the output of the above program −
List elements are: 10 20 30 40 50 List contains following elements 1 2 3 4 5
Example 2
Following is another example of the C++ ,std::list::get_allocator() function. Here, we are creating a list(type int) named num_list. Then, using the get_allocator() function, we are trying to retrieve an allocator associated with the current list.
#include<iostream> #include<list> using namespace std; int main(void) { //create a list list<int> num_list; //creating array using this list get_allocator int *arr; arr = num_list.get_allocator().allocate(5); arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; cout<<"List contains the following elements: "; for(int i = 0; i<5; i++) { cout<<arr[i]<<" "; } }
Output
This will generate the following output −
List contains the following elements: 10 20 30 40 50
Example 3
In this example, we create a list(type char) named char_list with an empty value. Then, using the get_allocator() function, we are trying to get the allocator associated with this list. We use the allocate() function to specify the number of objects 5 to allocate storage for the pointer to a nearby memory location.
#include<iostream> #include<list> using namespace std; int main(void) { //create a list list<char> char_list = {}; cout<<"Size of list: "<<char_list.size()<<endl; //creating array using this list get_allocator char *char_arr; char_arr = char_list.get_allocator().allocate(3); char_arr[0] = 'a'; char_arr[1] = 'b'; char_arr[2] = 'c'; cout<<"List contains the following elements: "; for(int i = 0; i<5; i++) { cout<<char_arr[i]<<" "; } }
Output
On executing the above program, it will produce the following output −
Size of list: 0 List contains the following elements: a b c ..