help on std::vector

T

Tim

Dear All,

std::vector seems using the copy constructor to allocate memory when
needed. Is it possible to control the memory allocation, such as by
using my own allocator? My question is as follow:

I have a smart pointer my_ptr, and the copy constructor uses deep
copy, however I want it to be shallow copy in std::vector when
reallocate memory. How can I implement it?

This seems a very challenging question as least for me. I appreciate
your kind help.

Best regards,

Tim
 
A

AnonMail2005

Dear All,

std::vector seems using the copy constructor to allocate memory when
needed. Is it possible to control the memory allocation, such as by
using my own allocator? My question is as follow:

I have a smart pointer my_ptr, and the copy constructor uses deep
copy, however I want it to be shallow copy in std::vector when
reallocate memory. How can I implement it?

This seems a very challenging question as least for me. I appreciate
your kind help.

Best regards,

Tim
1. You can use reserve on the vector, if you know the maximum number
of elements
that you'll use, to prevent re-allocation and copying.

2. Use a vector of raw pointers if the vector is being used as
temporary storage
(e.g. inside a function) to work with the elements.

3. Use a vector of some other pointer class that does shallow copy.

Hope that helps.
 
G

Gianni Mariani

Tim said:
Dear All,

std::vector seems using the copy constructor to allocate memory when
needed. Is it possible to control the memory allocation, such as by
using my own allocator? My question is as follow:

I have a smart pointer my_ptr, and the copy constructor uses deep
copy, however I want it to be shallow copy in std::vector when
reallocate memory. How can I implement it?

This seems a very challenging question as least for me. I appreciate
your kind help.

There are some simple things you can do but it's not trivial to get
right. I think you're going to need to implement your own container to
do this right.
 

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,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top