Allocator code in C++ Primer

P

pauldepstein

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
 
B

Bo Persson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top