forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex12.26.cpp
44 lines (35 loc) · 1.08 KB
/
ex12.26.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/***************************************************************************
* @file The code is for the exercises in C++ Primmer 5th Edition
* @author Alan.W
* @date 26 DEC 2013
* @remark
***************************************************************************/
//!
//! Exercise 12.26:
//! Rewrite the program on page 481 using an allocator.
//!
#include <iostream>
#include <memory>
int main()
{
//! create a allocator object and use it to handle string.
//! create a movable pointer to the address p points
std::allocator<std::string> alloc;
std::string* const p = alloc.allocate(5);
std::string* p_movable = p;
//! constuct each object using copy constructor
std::string word;
while(std::cin >> word && p_movable != p + 3)
{
alloc.construct(p_movable,word);
++p_movable;
}
//! move the movable pointer back home
p_movable = p;
//! print the strings constructed.
while(p_movable != p + 3)
std::cout << *p_movable++ <<"\n";
//! free the allocated memory.
delete[] p;
return 0;
}