[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Resizing Vector of shared pointers

joe

10/15/2008 4:28:00 PM

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
7 Answers

acehreli

10/15/2008 7:03:00 PM

0

On Oct 15, 9:28 am, joe <joeyc...@mail.com> wrote:

> 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

Thomas J. Gritzan

10/15/2008 7:05:00 PM

0

acehreli@gmail.com schrieb:
> On Oct 15, 9:28 am, joe <joeyc...@mail.com> wrote:
>
>> 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

Bo Persson

10/15/2008 7:07:00 PM

0

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


joseph cook

10/15/2008 11:13:00 PM

0


> 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

Stephen Horne

10/16/2008 2:32:00 AM

0

On Wed, 15 Oct 2008 09:28:01 -0700 (PDT), joe <joeycook@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 ;-)

James Kanze

10/16/2008 9:52:00 AM

0

On Oct 15, 6:28 pm, joe <joeyc...@mail.com> wrote:
> 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:james.kanze@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

Jeff Schwab

10/16/2008 5:53:00 PM

0

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.