获取一块临时内存。在C++ STL库中,有一个函数 获取临时缓冲区 这主要是用来获得一个临时块。
null
- 这个函数取一个大小为n的值,返回最大的可用缓冲区,最大的缓冲区大小为n,可以放入物理内存中。
- 此函数用于获取临时性内存,主要用于算法的操作,因为某些算法需要额外的空间才能正确执行。
- 一旦分配的内存块不再需要,则应通过调用将其释放 返回临时缓冲区。
语法:
pair(int*, ptrdiff_t) p = get_temporary_buffer(int)(required size)
参数:
- n:为其分配临时内存的T类型元素的数量。
- ptrdiff_t:它是一种积分类型。
返回: 函数返回第一对和第二对对象。分配内存时,第一个包含指向块中第一个元素的指针,第二个包含大小。如果未分配内存块,则第一对包含空指针,第二对包含零。
例1: 要计算数组中的偶数总数,并使用 获取临时缓冲区
Input : 8, 9, 2, 1, 10, 14, 37, 18, 17, 5Output : It contain 10 elements Sorted array is 1, 2, 5, 8, 9, 10, 14, 17, 18, 37Explanation:Step 1: initialize 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 10Step 3: now copy the elements in the temporary buffer uninitialized_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.
C++
// C++ 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 << "" ; for (i = 0; i < 10; i++) { cout << b[i] << " " ; } cout << "" ; 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: 5original 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.
C++
// C++ 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 characters are :b g p v y
申请: 算法通常需要临时空间才能正确执行。STL内部使用它有非常特殊的用途,比如 稳定分区 ,稳定排序和 就地合并 它们使用额外的临时内存来存储中间结果,如果有额外的内存可用,它们的运行时复杂性会更好。
本文由 希瓦尼·巴格尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END