Allocator code in C++ Primer

Discussion in 'C++' started by pauldepstein@att.net, May 8, 2009.

  1. Guest

    I saw this code in the 4th edition of C++ Primer (Lippman, Lajoie and
    Moo):

    allocator<string> alloc;
    string* sp = alloc.allocate(2);

    alloc.construct(sp+1, string(b, e)); // b and e are string iterators.

    I don't understand the reason for the "+1" in sp+1.

    Wouldn't alloc.construct(sp, string(b,e)); be simpler and more
    natural?

    Is there a reason for the "+1"?

    Many thanks for your help.


    Paul Epstein
    , May 8, 2009
    #1
    1. Advertising

  2. Bo Persson Guest

    wrote:
    > I saw this code in the 4th edition of C++ Primer (Lippman, Lajoie
    > and Moo):
    >
    > allocator<string> alloc;
    > string* sp = alloc.allocate(2);
    >
    > alloc.construct(sp+1, string(b, e)); // b and e are string
    > iterators.
    >
    > I don't understand the reason for the "+1" in sp+1.
    >
    > Wouldn't alloc.construct(sp, string(b,e)); be simpler and more
    > natural?
    >
    > Is there a reason for the "+1"?
    >


    I believe it is just to show that it can be done that way. You
    allocate space for two strings, and choose to construct the second
    one.


    Bo Persson
    Bo Persson, May 9, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Min-Koo Seo
    Replies:
    0
    Views:
    404
    Min-Koo Seo
    Jun 21, 2004
  2. tom_usenet
    Replies:
    0
    Views:
    524
    tom_usenet
    Jun 21, 2004
  3. Alex Vinokur
    Replies:
    16
    Views:
    12,249
    Alex Vinokur
    Aug 16, 2004
  4. Replies:
    3
    Views:
    1,206
    CBFalconer
    May 8, 2006
  5. xianwei
    Replies:
    2
    Views:
    344
    xianwei
    Sep 20, 2007
Loading...

Share This Page