Basic Knowledge Channel:http://blog.csdn.net/Xiejingfa/article/details/50955295
C + +:
1#include <iostream>2#include <vector>3#include <string>4 5 #defineAllocate_length 1000006 7 8 intMain ()9 {Ten One //allocator faster than NEW: Split allocation and initialization these two operations allocator one less step, new is typically performed two times (initialization and assignment); A - -std::clock_t start =0, end =0; the -Start =clock (); -STD::string*STR1 =NewSTD::string[allocate_length]; -Auto str6=str1; + for(inti =0; i < allocate_length; i++) - { +*str1++ ="Hello World"; A } at - Delete[]STR6; -End =clock (); -Std::cout << (Double(End-start)/clocks_per_sec) <<Std::endl; - - in - toStart =clock (); +STD::ALLOCATOR<STD::string>str_allocate; -STD::string*STR3 = Str_allocate.allocate (allocate_length);//allocate 20 strings of raw memory theAuto str4=STR3; * $ //method One: Use the default constructPanax Notoginseng for(inti =0; i < allocate_length; i++) - { theStr_allocate.construct (str3++,"Hello World"); + } A the //method Two: The adjoint algorithm using allocator (with N and no band respectively) + //Other algorithms: std::uninitialized_copy (iterator begin,iterator end,t value); - $ //Std::uninitialized_fill_n (str3,allocate_length, "Hello World"); $ - - //Str_allocate.destroy () calls the destructor of the object, but the memory is still controlled by allocator and needs to be freed the str_allocate.deallocate (str4,allocate_length); -End =clock ();Wuyi theStd::cout << (Double(End-start)/clocks_per_sec) <<Std::endl; - Wu - return 0; About}
C + + std::allocator<t> use