獲取一塊臨時內存。在C++ STL庫中,有一個函數get_temporary_buffer,該函數主要用於獲取臨時塊。
- 此函數的大小為n,並返回最大大小為n的可用緩衝區,該緩衝區可以裝入物理內存。
- 此函數用於獲取臨時性質的內存,該內存主要用於算法的操作,因為某些算法需要額外的空間才能正確執行。
- 一旦不再需要分配的存儲塊,則應通過調用return_temporary_buffer將其釋放。
用法:
pair(int*, ptrdiff_t) p = get_temporary_buffer(int)(required size)
參數:
- n:為其分配臨時內存的T類型的元素數。
- ptrdiff_t:它是整數類型。
返回:該函數返回第一對和第二對對象。分配內存後,第一個包含指向塊中第一個元素的指針,第二個包含大小。如果未分配內存塊,則第一對包含空指針,第二對包含零。
範例1:
要計算數組中的偶數總數,並使用get_temporary_buffer打印排序的數組
Input:8, 9, 2, 1, 10, 14, 37, 18, 17, 5 Output:It contain 10 elements Sorted array is 1, 2, 5, 8, 9, 10, 14, 17, 18, 37 Explanation: Step 1:initiliaze the array b[] first, we find the even number elements in an array using for loop[0-n-1] if(a[i]%2==0){ c++;} print the count of even number. Step 2:use get_temporary buffer to allocate the block of memory pair(int*, ptrdiff_t) p=get_temporary_buffer(int)(required size) here required size is 10 Step 3:now copy the elements in the temporary buffer unitialized_copy(b, b+p.second, p.first); now using for loop [0 to p.second-1] sort the array using sort function sort(p.first, p.first+p.second) and finally print the sorted array.
// CPP code to demonstrate the get_temporary_buffer
// to sort an array
#include <iostream>
#include <algorithm>
#include <memory>
using namespace std;
void sorting(int b[], int n)
{
int i, c = 0;
for (i = 0; i < n; i++) {
if (b[i] % 2 == 0) {
c++;
}
}
cout << "The total even numbers are: " << c << endl;
cout << "original array:"
<< " ";
cout << "\n";
for (i = 0; i < 10; i++) {
cout << b[i] << " ";
}
cout << "\n";
pair<int*, ptrdiff_t> p = get_temporary_buffer<int>(10);
// copy the contents in temporary buffer with pair
uninitialized_copy(b, b + p.second, p.first);
sort(p.first, p.first + p.second);
cout << "sorted array:" << endl;
for (i = 0; i < p.second; i++) {
cout << p.first[i] << " ";
}
}
// driver program to test above function
int main()
{
int b[] = { 8, 9, 2, 1, 10, 14, 37, 18, 17, 5 };
int n = sizeof(b) / sizeof(b[0]);
sorting(b, n);
return 0;
}
輸出:
The total even numbers are:5 original array: 8 9 2 1 10 14 37 18 17 5 sorted array: 1 2 5 8 9 10 14 17 18 37
範例2:
使用get_temporary_buffer和return_temporary_buffer按字母順序對字符串進行排序
Input:'b', 'g', 'y', 'v', 'p' Output:b g p v y This will print the contents in an increasing order of alphabets.
// CPP code to sort the characters
// alphabetically using std::get_temporary_buffer
#include <iostream>
#include <algorithm>
#include <memory>
#include <string.h>
using namespace std;
void sorting(char b[], int n)
{
int i;
pair<char*, ptrdiff_t> p = get_temporary_buffer<char>(n);
// copy the contents in temporary buffer with pair
uninitialized_copy(b, b + p.second, p.first);
// sort char array
sort(p.first, p.first + p.second);
cout << "sorted characters are:" << endl;
for (i = 0; i < p.second; i++) {
cout << p.first[i] << " ";
}
// to release the temporary buffer
return_temporary_buffer(p.first);
}
// driver program to test above function
int main()
{
char str[] = { 'b', 'g', 'y', 'v', 'p' };
int c;
c = strlen(str);
sorting(str, c);
return 0;
}
輸出:
sorted charaters are: b g p v y
應用:算法通常需要臨時空間才能正確執行。它具有非常特殊的用途,STL在內部將其用於stable_partition,stable_sort和inplace_merge等算法,它們使用額外的臨時內存來存儲中間結果,並且如果有額外的內存可用,它們的運行時複雜性會更好。
相關用法
注:本文由純淨天空篩選整理自 std::get_temporary_buffer in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。