1. 程式人生 > >容器配置器(allocator)(二)

容器配置器(allocator)(二)

#include <iostream>
#include <list>

namespace ClassFoo{
	void ListGetAllocatorExample1(){
		std::list<int> foo;
		int * p;

		// 用記憶體分配器分配包含5個元素的陣列
		p = foo.get_allocator().allocate(5);

		// 為陣列中的每個元素賦值
		for (int i = 0; i<5; ++i) p[i] = i;

		std::cout << "The allocated array contains:";
		for (int i = 0; i<5; ++i) std::cout << ' ' << p[i];
		std::cout << '\n';

		foo.get_allocator().deallocate(p, 5);
	}
}

int main()
{
	ClassFoo::ListGetAllocatorExample1();
	return 0;
}