C++ Primer 5th笔记(chap 12 动态内存)allocator类
生活随笔
收集整理的這篇文章主要介紹了
C++ Primer 5th笔记(chap 12 动态内存)allocator类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 標(biāo)準(zhǔn)庫allocator類及其算法
| allocator <.T> a | 定義了一個(gè)名為a的allocator對(duì)象,他可以為類型T的對(duì)象分配內(nèi)存 |
| a.allocate(n) | 分配一段原始的、未構(gòu)造的內(nèi)存,保存n個(gè)類型為T的對(duì)象 |
| a.deallocate(n) | 釋放從T*指針p中地址開始的內(nèi)存,這塊內(nèi)存保存了n個(gè)類型為T的對(duì)象;p必須是一個(gè)先前由allocator返回的指針,且n必須是p創(chuàng)建時(shí)所要求的大小。調(diào)用dealocator之前,用戶必須對(duì)每個(gè)在這塊內(nèi)存中創(chuàng)建的對(duì)象調(diào)用destroy |
| a.construct(p,args) | p必須是一個(gè)類型為T*的指針,指向一塊原始內(nèi)存;arg被傳遞給類型為T的構(gòu)造函數(shù),用來在p指向的內(nèi)存中構(gòu)造一個(gè)對(duì)象 |
| a.destroy§ | p為T*類型的指針,此算法對(duì)p指向的對(duì)象執(zhí)行析構(gòu)函數(shù) |
注意:
const size_t n = 100;allocator<string> allocStr; // object that can allocate stringsauto p = allocStr.allocate(n); // allocate n unconstructed strings//cout << *p << endl;auto q = p; // q will point to one past the last constructed elementallocStr.construct(q++); // *q is the empty stringcout << *(q - 1) << endl;allocStr.construct(q++, 10, 'c'); // *q is cccccccccccout << *(q - 1) << endl;//allocStr.construct(q++, "hi"); // *q is hi!//cout << *(q - 1) << endl;int nCount = 0;cout << *p << endl; // ok: uses the string output operatorwhile (q != p) {nCount++;allocStr.destroy(--q); // free the strings we actually allocated}cout << nCount << endl; // ok: uses the string output operator allocStr.deallocate(p, n); // return the memory we allocatedp = allocStr.allocate(n); // allocate n unconstructed stringsstring s;q = p; // q points to the memory for first stringifstream in("E:/temp/storyDataFile");while (in >> s && q != p + n)allocStr.construct(q++, s); // construct only as many strings as we needsize_t size = q - p; // remember how many strings we read// use the arraycout << "read " << size << " strings" << endl;for (q = p + size - 1; q != p; --q)allocStr.destroy(q); // free the strings we allocatedallocStr.deallocate(p, n); // return the memory we allocatedin.close();in.open("E:/temp/storyDataFile");p = new string[n]; // construct n empty stringsq = p; // q points to the first stringwhile (in >> s && q != p + n)*q++ = s; // assign a new value to *qsize = q - p; // remember how many strings we readcout << "read " << size << " strings" << endl;2. "copy和填充未初始化的內(nèi)存"算法
| uninitialized_copy(b,e,b2) | 將迭代器b和e之間的輸入,拷貝到迭代器b2指定的未構(gòu)造的原始內(nèi)存中,b2指向的內(nèi)存必須足夠大,能夠容納輸入序列中元素的拷貝 |
| uninitialized_copy_n(b,n,b2) | 同上,從b開始拷貝n個(gè)元素到b2 |
| uninitialized_fill(b,e,t) | 在迭代器b和e指定的原始內(nèi)存范圍中創(chuàng)建對(duì)象,對(duì)象的值,均為t的拷貝 |
| uninitialized_fill_n(b,n,t) | 從b指向的內(nèi)存地址開始創(chuàng)建n個(gè)對(duì)象,b必須指向足夠大的內(nèi)存 |
使用示例:
vector<int> vi{ 1,2,3,4,5,6,7,8,9 };allocator<int> alloc;// allocate twice as many elements as vi holds auto p = alloc.allocate(vi.size() * 2);// construct elements starting at p as copies of elements in vi auto q = uninitialized_copy(vi.begin(), vi.end(), p);// initialize the remaining elements to 42 uninitialized_fill_n(q, vi.size(), 42);for (size_t i = 0; i != vi.size(); ++i) cout << *(p + i) << " "; cout << endl;for (size_t i = 0; i != vi.size(); ++i) cout << *(q + i) << " "; cout << endl;alloc.deallocate(p, vi.size());retsult:
1 2 3 4 5 6 7 8 9 42 42 42 42 42 42 42 42 42總結(jié)
以上是生活随笔為你收集整理的C++ Primer 5th笔记(chap 12 动态内存)allocator类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Primer 5th笔记(cha
- 下一篇: C++ Primer 5th笔记(cha