C++ Primer(第5版) 练习 12.26
练习 12.26 用allocator重写第427中的程序。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************
> File Name: ex12.26.cpp
> Author:
> Mail:
> Created Time: Fri 19 Apr 2024 11:26:30 AM CST
************************************************************************/
#include<iostream>
#include<string>
#include<memory>
using namespace std;
int main(){
allocator<string> str;
auto const p = str.allocate(10);
auto q = p;
string word;
cout<<"Enter string: ";
while(q != p + 10 && cin>>word){
str.construct(q++, word);
}
const size_t len = q - p;
cout<<"String: ";
for(size_t i = 0; i < len; ++i){
cout<<*(p + i)<<" ";
}
cout<<endl;
while(q != p){
str.destroy(--q);
}
str.deallocate(p, 10);
return 0;
}