欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

C++ Primer 5th笔记(chap 12 动态内存)allocator类

发布时间:2025/3/21 c/c++ 30 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C++ Primer 5th笔记(chap 12 动态内存)allocator类 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1. 标准库allocator类及其算法

算法说明
allocator <.T> a定义了一个名为a的allocator对象,他可以为类型T的对象分配内存
a.allocate(n)分配一段原始的、未构造的内存,保存n个类型为T的对象
a.deallocate(n)释放从T*指针p中地址开始的内存,这块内存保存了n个类型为T的对象;p必须是一个先前由allocator返回的指针,且n必须是p创建时所要求的大小。调用dealocator之前,用户必须对每个在这块内存中创建的对象调用destroy
a.construct(p,args)p必须是一个类型为T*的指针,指向一块原始内存;arg被传递给类型为T的构造函数,用来在p指向的内存中构造一个对象
a.destroy§p为T*类型的指针,此算法对p指向的对象执行析构函数

注意:

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和填充未初始化的内存"算法

算法说明
uninitialized_copy(b,e,b2)将迭代器b和e之间的输入,拷贝到迭代器b2指定的未构造的原始内存中,b2指向的内存必须足够大,能够容纳输入序列中元素的拷贝
uninitialized_copy_n(b,n,b2)同上,从b开始拷贝n个元素到b2
uninitialized_fill(b,e,t)在迭代器b和e指定的原始内存范围中创建对象,对象的值,均为t的拷贝
uninitialized_fill_n(b,n,t)从b指向的内存地址开始创建n个对象,b必须指向足够大的内存

使用示例:

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

总结

以上是生活随笔为你收集整理的C++ Primer 5th笔记(chap 12 动态内存)allocator类的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。