- 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 - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ multiset Library - get_allocator() Function
Description
The multiset::get_allocator() function in C++ STL is used to return a copy of the allocator object that is associated with the multiset container. This allocator object is used to handle the memory allocation and deallocation for the multiset.
Syntax
Following is the syntax of multiset::get_allocator() function −
allocator_type get_allocator() const;
Parameters
The get_allocator() function does not accept any parameter.
Return value
The get_allocator() function returns the allocator object associated with the multiset container.
Exceptions
If an exception is thrown, the container remains unchanged.
Time complexity
The time complexity of multiset::get_allocator() function is O(1).
Examples of multiset::get_allocator() Function
The following examples demonstrate the usage of multiset::get_allocator() function in multiset −
Allocating Multiple Elements Using get_allocator()
In this example, we have used the get_allocator() function to get the allocator from multiset nums and allocated memory for 5 integer elements.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> nums = {10, 20, 30};
cout << "Multiset elements: ";
for (const auto &num : nums)
cout << " " << num;
cout << endl;
// Get the allocator
auto alloc = nums.get_allocator();
// Allocate memory for 5 integers
int *p = alloc.allocate(5);
// Construct values in allocated memory
for (int i = 0; i < 5; i++)
alloc.construct(p + i, i * 10);
cout << "Allocated numbers: ";
for (int i = 0; i < 5; i++)
cout << p[i] << " ";
cout << endl;
// Destroy and deallocate
for (int i = 0; i < 5; i++)
alloc.destroy(p + i);
alloc.deallocate(p, 5);
return 0;
}
The output of the above code is given below −
Multiset elements: 10 20 30 Allocated numbers: 0 10 20 30 40
Using get_allocator() with String Multiset
In this example, we have used the get_allocator() function to get the allocator from a string multiset words and allocated memory for 5 strings.
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
multiset<string> words = {"apple", "banana", "cherry"};
cout << "Multiset elements:";
for (const auto &word : words)
cout << " " << word;
cout << endl;
// Get the allocator
auto alloc = words.get_allocator();
// Allocate memory for 5 strings
string *p = alloc.allocate(5);
// Construct values in allocated memory
alloc.construct(p, "one");
alloc.construct(p + 1, "two");
alloc.construct(p + 2, "three");
alloc.construct(p + 3, "four");
alloc.construct(p + 4, "five");
cout << "Allocated strings:";
for (int i = 0; i < 5; i++)
cout << " " << p[i];
cout << endl;
// Destroy and deallocate
for (int i = 0; i < 5; i++)
alloc.destroy(p + i);
alloc.deallocate(p, 5);
return 0;
}
The output of the above code is given below −
Multiset elements: apple banana cherry Allocated strings: one two three four five