Debian Codegraph @

yo/containers/examples/allocator.cc

raw
1 #include <iostream>
2 #include <vector>
3 #include <string>
4
5 using namespace std;
6
7 int main()
8 {
9 vector<string> vs;
10
11 auto allocator = vs.get_allocator(); // get the allocator
12
13 string *sp = allocator.allocate(3); // alloc. space for 3 strings
14
15 allocator.construct(&sp[0], "hello world"); // initialize 1st string
16 allocator.construct(&sp[1], sp[0]); // use the copy constructor
17 allocator.construct(&sp[2], 12, '='); // string of 12 = chars
18
19 cout << sp[0] << '\n' << // show the strings
20 sp[1] << '\n' <<
21 sp[2] << '\n' <<
22 "could have allocated " << allocator.max_size() << " strings\n";
23
24 for (size_t idx = 0; idx != 3; ++idx)
25 allocator.destroy(sp + idx); // delete the string's
26 // contents
27
28 allocator.deallocate(sp, 3); // and delete sp itself again.
29 }