C + + Primer The 18th chapter Vector's re-implementation and bug correction

Source: Internet
Author: User

C++primer 18th. 1.2 In introducing the allocator class, an example of a vector modeled in the standard library was given. Feel the sample code is very good, but I found a bug, shared with everyone.

According to the author's sample program, compile the program always in the Alloc.construct () function error, different IDE may be prompted by different reasons, I am undefined reference to ' vector<std::string>: Alloc '.

Think about it, it should be related to the static property of the Alloc member in the class vector, so there are two ways to fix it:

1) Delete the static keyword

2) Add:template<class t> std::allocator<t> in global scope vector<t>::alloc;

The test source code is as follows:

1#include <iostream>2#include <string>3#include <memory>4 using namespacestd;5 6 std::p trdiff_t MAX (std::p trdiff_t A, std::p trdiff_t B)7 {8     returna > B?a:b;9 }Ten  OneTemplate <classT>classvector{ A  Public: -Vector (): Elements (0), First_free (0), End (0) {} -     voidPush_back (Constt&); the  - Private: -     StaticStd::allocator<t>Alloc; -     voidreallocate (); +  Public: -Telements; +TFirst_free; ATend; at }; -  -template<classT> std::allocator<t> vector<t>::alloc;//If there is no such sentence, the compiler error, but if the class vector to remove the static keyword, this sentence can be deleted -  -Template <classT> - voidVector<t>::p Ush_back (Constt&t) in { -     if(First_free = =end) to reallocate (); + alloc.construct (first_free,t); -++First_free; the } *  $Template <classT>Panax Notoginseng voidVector<t>:: Reallocate () - { thestd::p trdiff_t size = first_free-elements; +std::p trdiff_t newcapacity =2* MAX (size,1); A  theT * newelements =alloc.allocate (newcapacity); +  - uninitialized_copy (Elements, First_free, newelements); $  $      for(T * p = first_free; p! = elements; Alloc.destroy (--p)); -  -     if(elements) theAlloc.deallocate (Elements, End-elements); - Wuyielements =newelements; theFirst_free = elements +size; -End = elements +newcapacity; Wu } -  About intMain () $ { -vector<string>str; -     strings; -      for(inti =0; I <5; ++i) A     { +Cin>>s; the Str.push_back (s); -     } $      for(string* p = str.elements; P! = Str.first_free; ++p) thecout<<*p<<Endl; the     return 0; the}

Although know the solution, but for the cause of the bug is not very clear, welcome to message exchange and advice.

C + + Primer The 18th chapter Vector's re-implementation and bug correction

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.