系统相关
首页 > 系统相关> > C++ Primer 5th笔记(chap 12)动态内存 allocator类

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 strings
    auto p = allocStr.allocate(n);   // allocate n unconstructed strings

    //cout << *p << endl;

    auto q = p; // q will point to one past the last constructed element
    allocStr.construct(q++);         // *q is the empty string
    cout << *(q - 1) << endl;

    allocStr.construct(q++, 10, 'c'); // *q is cccccccccc
    cout << *(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 operator
    while (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 allocated

    p = allocStr.allocate(n);   // allocate n unconstructed strings
    string s;
    q = p;                   // q points to the memory for first string
    ifstream in("E:/temp/storyDataFile");
    while (in >> s && q != p + n)
        allocStr.construct(q++, s); // construct only as many strings as we need
    size_t size = q - p;         // remember how many strings we read

    // use the array
    cout << "read " << size << " strings" << endl;

    for (q = p + size - 1; q != p; --q)
        allocStr.destroy(q);         // free the strings we allocated
    allocStr.deallocate(p, n);       // return the memory we allocated

    in.close();
    in.open("E:/temp/storyDataFile");
    p = new string[n];            // construct n empty strings
    q = p;                        // q points to the first string
    while (in >> s && q != p + n)
        *q++ = s;                 // assign a new value to *q
    size = q - p;                 // remember how many strings we read

    cout << "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

标签:12,vi,5th,42,allocate,chap,内存,allocator,size
来源: https://blog.csdn.net/thefist11cc/article/details/113799485