forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.26.cpp
38 lines (31 loc) · 746 Bytes
/
12.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
#include <string>
#include <iostream>
#include <memory>
using std::string;
using std::cin; using std::cout; using std::endl;
int main() {
std::allocator<string> alloc;
constexpr size_t n = 10; // capacity
auto const p = alloc.allocate(n); //string *const p
string s;
auto q = p; //string *q
while (cin >> s && q != p + n)
alloc.construct(q++, s);
const size_t sz = q - p; // size
for (auto pos = p; pos != q; ++pos)
std::cout << *pos << std::endl;
while (q != p)
alloc.destroy(--q);
alloc.deallocate(p, n);
/**
string *const p = new string[n];
string s;
string *q = p;
while (cin >> s && q != p + n)
*q++ = s;
const size_t size = q - p;
// use the array
delete[] p;
/**/
return 0;
}