C++ Primer 练习 12.26 题

来源:互联网 发布:unity3d minecraft 编辑:程序博客网 时间:2024/06/03 19:24

C++ primer 第 12.26 练习题:

    用allocator重写第427页中的程序。


#include <string>using std::string;#include <iostream>using std::cin;using std::cout;using std::endl;#include <memory>using std::allocator;#include <vector>using std::vector;int main(){allocator<string> alloc;size_t i = 9;auto const p = alloc.allocate(i);auto q = p;string s;while (q != p + i && cin >> s)alloc.construct(q++, s);//构造元素之后 递增指针const size_t size = q - p;cout << size << endl;for (size_t i = 0; i != size; i++)cout << *(p + i) << endl;while (q != p)alloc.destroy(--q);alloc.deallocate(p, i);return 0;}

以上。

原创粉丝点击