468,272 Members | 1,980 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

help on std::vector

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

Sep 3 '07 #1
2 1772
On Sep 3, 2:24 pm, Tim <Tian.Xiao.2...@gmail.comwrote:
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.

Sep 3 '07 #2
Tim wrote:
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.
Sep 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Michael Hopkins | last post: by
8 posts views Thread by Ross A. Finlayson | last post: by
32 posts views Thread by zl2k | last post: by
9 posts views Thread by aaragon | last post: by
6 posts views Thread by lokchan | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.