473,374 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,374 software developers and data experts.

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 1961
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
8
by: Lionel B | last post by:
On my platform I find that the std::vector<boolspecialisation incurs a significant performance hit in some circumstances (when compared, say, to std::vector<intprogrammed analagously). Is it...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
3
by: DevNull | last post by:
I have a program where we load a mapfile, comprised of a .csv with numbers which represent object types at a given position. Each position is in the map is called a Cell, a cell contains only a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.