C + + std::allocator<t> use

Source: Internet
Author: User

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&LT;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.