473,411 Members | 1,995 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,411 software developers and data experts.

Resizing Vector of shared pointers

joe
Brain Block.

I thought there was a way to do this in less lines (1 line?):

std::vector<boost::shared_ptr<T m_vec;

for (int i =0; i<totalSize; ++i)
{
m_vec.push_back(new T);
}

but my brain isn't letting me remember how right now. Anyone out
there want to help?

Joe C
Oct 15 '08 #1
7 4894
On Oct 15, 9:28*am, joe <joeyc...@mail.comwrote:
I thought there was a way to do this in less lines (1 line?):

std::vector<boost::shared_ptr<T m_vec;

for (int i =0; i<totalSize; ++i)
{
* *m_vec.push_back(new T);

}
m_vec.resize(totalSize);

Ali
Oct 15 '08 #2
ac******@gmail.com schrieb:
On Oct 15, 9:28 am, joe <joeyc...@mail.comwrote:
>I thought there was a way to do this in less lines (1 line?):

std::vector<boost::shared_ptr<T m_vec;

for (int i =0; i<totalSize; ++i)
{
m_vec.push_back(new T);

}

m_vec.resize(totalSize);
This will fill the vector with empty shared_ptr, while the above loop
will fill the vector with allocated objects. So they won't do the same.

--
Thomas
Oct 15 '08 #3
joe wrote:
Brain Block.

I thought there was a way to do this in less lines (1 line?):

std::vector<boost::shared_ptr<T m_vec;

for (int i =0; i<totalSize; ++i)
{
m_vec.push_back(new T);
}

but my brain isn't letting me remember how right now. Anyone out
there want to help?
It depends somewhat on T, and what you are doing with it. If you
want/need to have a certain number of distinct Ts, you just need to
use 'new' that number of times. If it is ok to share a single T, you
can do that in the constructor.
Bo Persson
Oct 15 '08 #4
It depends somewhat on T, and what you are doing with it. If you
want/need to have a certain number of distinct Ts, you just need to
use 'new' that number of times. If it is ok to share a single T, you
can do that in the constructor.

Bo Persson
Oh, well, I thought somehow I could create distinct T's in a
constructor line...I must have had too much pumpkin wine.

Joe C
Oct 15 '08 #5
On Wed, 15 Oct 2008 09:28:01 -0700 (PDT), joe <jo******@mail.com>
wrote:
>std::vector<boost::shared_ptr<T m_vec;

for (int i =0; i<totalSize; ++i)
{
m_vec.push_back(new T);
}

but my brain isn't letting me remember how right now. Anyone out
there want to help?
I can do the loop in one line, but you won't like it...

while (m_vec.size () < totalSize) m_vec.push_back (new T);

I said you wouldn't like it ;-)

Oct 16 '08 #6
On Oct 15, 6:28 pm, joe <joeyc...@mail.comwrote:
I thought there was a way to do this in less lines (1 line?):
std::vector<boost::shared_ptr<T m_vec;
for (int i =0; i<totalSize; ++i)
{
m_vec.push_back(new T);
}
but my brain isn't letting me remember how right now. Anyone
out there want to help?
How about std::fill_n with a back inserter and a
boost::function_output_iterator. For that matter, I'm pretty
sure that I saw an iterator adapter somewhere which stopped
after a maximum of n times; use that with the
function_output_iterator, and you should be able to use the two
iterator form of the constructor directly.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 16 '08 #7
James Kanze wrote:
I'm pretty
sure that I saw an iterator adapter somewhere which stopped
after a maximum of n times
I've always wondered why there is no counting_iterator in the standard
library. It's very handy.
Oct 16 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sean Dettrick | last post by:
Hi, Can anyone please tell me if the following is possible, and if so, what is the correct syntax? I'd like to resize a vector (and access other vector member functions) via a pointer to that...
6
by: Puvendran | last post by:
Hi, I am trying to create an array of pointers to a vector which holds values of a user defined class. The application involved needs multiple arrays to point the same vector ie class message...
14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
46
by: Blue Ocean | last post by:
How do I do it? I'm doing a practice problem which includes me implementing a set of ints as a class. I don't want to use vector because that would be cheating. ;) I don't want to spend...
5
by: christophe (dot) poucet (at) gmail (dot) com | last post by:
Hello, I noticed there is a flaw in the vector implementation of g++ (3.4). Basically when you erase an element from a vector, it calls the wrong destructor. In most cases this is not such a...
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
4
by: sreedhar.cs | last post by:
Hi all, In my application,I want to place a vector in a specific location in shared memory.(a user supplied pointer). I understand that the STL allocator mechanism places the data objects within...
3
by: Dave | last post by:
A quick question: vector <T *test_vec; T testT; test_vec.push_back(&testT); T * testT2p = new T; test_vec.push_back(testT2p); Now at the end do I need to explicitly delete the vector...
13
by: smp | last post by:
Does anyone know why making a vector of pointers is so much less efficient than a vector of objects? For a simple example: int num = 20; vector<int*v_int_ptr; v_int_ptr.reserve(num); ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.