获取一块临时内存。在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
#include
#include
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 p = get_temporary_buffer(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
#include
#include
#include
using namespace std;
void sorting(char b[], int n)
{
int i;
pair p = get_temporary_buffer(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,steady_sort和inplace_merge之类的算法,它们使用额外的临时内存来存储中间结果,如果有额外的内存可用,它们的运行时复杂性会更好。
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。