#include<iostream>
#include<vector>
#include<queue>
#include<unordered_map>
#include <memory>
#include <string>
using namespace std;
namespace allocator_ {
int test_allocator_1()
{
std::allocator<std::string> alloc;
int n{ 5 };
auto const p = alloc.allocate(n);
auto q = p;
alloc.construct(q++);
alloc.construct(q++, 10, 'c');
alloc.construct(q++, "hi");
std::cout << *p << std::endl;
std::cout << p[0] << std::endl;
std::cout << p[1] << std::endl;
std::cout << p[2] << std::endl;
while (q != p) {
alloc.destroy(--q);
}
alloc.deallocate(p, n);
return 0;
}
int test_allocator_2()
{
std::vector<int> vi{ 1, 2, 3, 4, 5 };
std::allocator<int> alloc;
auto p = alloc.allocate(vi.size() * 2);
auto q = std::uninitialized_copy(vi.begin(), vi.end(), p);
std::uninitialized_fill_n(q, vi.size(), 42);
for (int i = 0; i < vi.size() * 2; i++) {
cout << p[i] << endl;
}
return 0;
}
int test_allocator_3()
{
std::cout << std::endl;
std::allocator<int> intAlloc;
std::cout << "intAlloc.max_size(): " << intAlloc.max_size() << std::endl;
int* intArray = intAlloc.allocate(100);
std::cout << "intArray[4]: " << intArray[4] << std::endl;
intArray[4] = 2011;
std::cout << "intArray[4]: " << intArray[4] << std::endl;
intAlloc.deallocate(intArray, 100);
std::cout << std::endl;
std::allocator<double> doubleAlloc;
std::cout << "doubleAlloc.max_size(): " << doubleAlloc.max_size() << std::endl;
std::cout << std::endl;
std::allocator<std::string> stringAlloc;
std::cout << "stringAlloc.max_size(): " << stringAlloc.max_size() << std::endl;
std::string* myString = stringAlloc.allocate(3);
stringAlloc.construct(myString, "Hello");
stringAlloc.construct(myString + 1, "World");
stringAlloc.construct(myString + 2, "!");
std::cout << myString[0] << " " << myString[1] << " " << myString[2] << std::endl;
stringAlloc.destroy(myString);
stringAlloc.destroy(myString + 1);
stringAlloc.destroy(myString + 2);
stringAlloc.deallocate(myString, 3);
std::cout << std::endl;
return 0;
}
int test_allocator_4()
{
std::allocator<int> a1;
int* a = a1.allocate(1);
a1.construct(a, 7);
std::cout << a[0] << '\n';
a1.deallocate(a, 1);
std::allocator<std::string> a2;
decltype(a1)::rebind<std::string>::other a2_1;
std::allocator_traits<decltype(a1)>::rebind_alloc<std::string> a2_2;
std::string* s = a2.allocate(2);
a2.construct(s, "foo");
a2.construct(s + 1, "bar");
std::cout << s[0] << ' ' << s[1] << '\n';
a2.destroy(s);
a2.destroy(s + 1);
a2.deallocate(s, 2);
return 0;
}
}
int main() {
allocator_::test_allocator_3();
}