473,509 Members | 2,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding new element to STL Vecotr - Efficiently

Hi

I want to increase my vector by one without supplying
any object as parameter (I want stl to use default constructor)

e.g.

// defined outside
vector<U> V;

void f()
{
U my_u;
V.push_back(U);
U& RU = V.back();
}

In the above example the default contructor of U was called
and later copy constructor ! this is inefficient !

I need something like that :

void f()
{
V.push_back();
U& RU = V.back();
}

Can I do it somehow ?

Thanks
Shimon

Oct 18 '05 #1
8 1782
Sh**********@gmail.com wrote:
Hi

I want to increase my vector by one without supplying
any object as parameter (I want stl to use default constructor)

e.g.

// defined outside
vector<U> V;

void f()
{
U my_u;
V.push_back(U);
U& RU = V.back();
}

In the above example the default contructor of U was called
and later copy constructor ! this is inefficient !

I need something like that :

void f()
{
V.push_back();
U& RU = V.back();
}

Can I do it somehow ?


You might try

V.resize( V.size()+1 );
Best

Kai-Uwe Bux

Oct 18 '05 #2
"Kai-Uwe Bux" <jk********@gmx.net> schrieb im Newsbeitrag
news:dj**********@murdoch.acc.Virginia.EDU...
Sh**********@gmail.com wrote:
Hi

I want to increase my vector by one without supplying
any object as parameter (I want stl to use default constructor)

e.g.

// defined outside
vector<U> V;

void f()
{
U my_u;
V.push_back(U);
U& RU = V.back();
}

In the above example the default contructor of U was called
and later copy constructor ! this is inefficient !

I need something like that :

void f()
{
V.push_back();
U& RU = V.back();
}

Can I do it somehow ?


You might try

V.resize( V.size()+1 );
Best

Kai-Uwe Bux


Isn't this the same?

U my_u;
V.push_back(U);
U& RU = V.back();

One constructor and one copy constructor.

V.resize( V.size()+1 );

One constructor and one copy constructor, too....
The constructor is called because the default argument is "T x = T()".

But I can't think of a solution because the containers always copy the
objects. The function push_back is not overloaded.... if it were overloaded,
we could call it perhaps without argument and it would add a
default-constructed object at the end. And it could return an iterator to
the inserted element. Then we would not have to call "back()".

Greetings Chris
Oct 18 '05 #3
Sh**********@gmail.com wrote:
Hi

I want to increase my vector by one without supplying
any object as parameter (I want stl to use default constructor)

e.g.

// defined outside
vector<U> V;

void f()
{
U my_u;
V.push_back(U);
U& RU = V.back();
}

In the above example the default contructor of U was called
and later copy constructor ! this is inefficient !

I need something like that :

void f()
{
V.push_back();
U& RU = V.back();
}

Can I do it somehow ?


How about a vector of (smart) pointers to U objects? Then, although a
copy is still done, it's trivial because it's only copying a pointer
and you control when and how construction is done:

class U {};

void Foo( std::vector<U*>& v )
{
v.push_back( new U );
}

Cheers! --M

Oct 18 '05 #4
ben
mlimber wrote:
Sh**********@gmail.com wrote:
Hi

I want to increase my vector by one without supplying
any object as parameter (I want stl to use default constructor)

e.g.

// defined outside
vector<U> V;

void f()
{
U my_u;
V.push_back(U);
U& RU = V.back();
}

In the above example the default contructor of U was called
and later copy constructor ! this is inefficient !

I need something like that :

void f()
{
V.push_back();
U& RU = V.back();
}

Can I do it somehow ?

How about a vector of (smart) pointers to U objects? Then, although a
copy is still done, it's trivial because it's only copying a pointer
and you control when and how construction is done:

class U {};

void Foo( std::vector<U*>& v )
{
v.push_back( new U );
}

Cheers! --M


This way you add another layer to element access. smart pointers usually
have house keeping tasks for every access (for example, check for
emptiness, reference counting, etc) and this can be expensive.

So this might end up incurring more overhead than just a copy
constructor call.

Ben
Oct 18 '05 #5
ben wrote:
mlimber wrote:
Sh**********@gmail.com wrote:
Hi

I want to increase my vector by one without supplying
any object as parameter (I want stl to use default constructor)

e.g.

// defined outside
vector<U> V;

void f()
{
U my_u;
V.push_back(U);
U& RU = V.back();
}

In the above example the default contructor of U was called
and later copy constructor ! this is inefficient !

I need something like that :

void f()
{
V.push_back();
U& RU = V.back();
}

Can I do it somehow ?

How about a vector of (smart) pointers to U objects? Then, although a
copy is still done, it's trivial because it's only copying a pointer
and you control when and how construction is done:

class U {};

void Foo( std::vector<U*>& v )
{
v.push_back( new U );
}

Cheers! --M


This way you add another layer to element access. smart pointers usually
have house keeping tasks for every access (for example, check for
emptiness, reference counting, etc) and this can be expensive.

So this might end up incurring more overhead than just a copy
constructor call.

Ben


True, but that is entirely dependent on the type of smart pointer
(boost::shared_ptr doesn't do any extra work for normal access by * and
-> if BOOST_ASSERTs are disabled, and std::auto_ptr, for instance,
incurs no run-time overhead, though, of course, it can't be used in
standard containers), and if worst came to worst, one could use raw
pointers, as I did in the example above.

The bigger question here, of course, is about premature optimization --
both in the OP and in the case of smart pointers vs. raw pointers.

Cheers! --M

Oct 18 '05 #6
Sh**********@gmail.com wrote:
Hi

I want to increase my vector by one without supplying
any object as parameter (I want stl to use default constructor)

e.g.

// defined outside
vector<U> V;

void f()
{
U my_u;
V.push_back(U);
U& RU = V.back();
}

In the above example the default contructor of U was called
and later copy constructor ! this is inefficient !

I need something like that :

void f()
{
V.push_back();
U& RU = V.back();
}

Can I do it somehow ?


Yes.

Vector<U> v;
U& r = v.Add();

see

http://upp.sf.net
http://upp.sourceforge.net/src$Vector$en-us.html

Actually, this was the exact problem that forced me to avoid using STL
(other being the copy-constructible requirement).

Mirek
Oct 18 '05 #7

Sh**********@gmail.com wrote:
// defined outside
vector<U> V;

void f()
{
U my_u;
V.push_back(U);
U& RU = V.back();
}

In the above example the default contructor of U was called
and later copy constructor ! this is inefficient !


Can't help you with your actual question, but another thing to keep in
mind is this:

Make sure the vector has some room to grow by using v.reserve(n); at
some point (where n>1). That way push_back() is not going to cause even
more overhead than you actually see already.

Cheers,
Andre

Oct 18 '05 #8
mlimber wrote:
ben wrote:
mlimber wrote:
> Sh**********@gmail.com wrote:
>
>>Hi
>>
>>I want to increase my vector by one without supplying
>>any object as parameter (I want stl to use default constructor)
>>
>>e.g.
>>
>>// defined outside
>>vector<U> V;
>>
>>void f()
>>{
>>U my_u;
>>V.push_back(U);
>>U& RU = V.back();
>>}
>>
>>In the above example the default contructor of U was called
>>and later copy constructor ! this is inefficient !
>>
>>I need something like that :
>>
>>void f()
>>{
>>V.push_back();
>>U& RU = V.back();
>>}
>>
>>Can I do it somehow ?
>
>
> How about a vector of (smart) pointers to U objects? Then, although a
> copy is still done, it's trivial because it's only copying a pointer
> and you control when and how construction is done:
>
> class U {};
>
> void Foo( std::vector<U*>& v )
> {
> v.push_back( new U );
> }
>
> Cheers! --M
>


This way you add another layer to element access. smart pointers usually
have house keeping tasks for every access (for example, check for
emptiness, reference counting, etc) and this can be expensive.

So this might end up incurring more overhead than just a copy
constructor call.

Ben


True, but that is entirely dependent on the type of smart pointer
(boost::shared_ptr doesn't do any extra work for normal access by * and
-> if BOOST_ASSERTs are disabled, and std::auto_ptr, for instance,
incurs no run-time overhead, though, of course, it can't be used in
standard containers), and if worst came to worst, one could use raw
pointers, as I did in the example above.

The bigger question here, of course, is about premature optimization --
both in the OP and in the case of smart pointers vs. raw pointers.


Actually, the biggest question should be whether the OP needs/wants copy or
reference semantics for the objects in the container. Changing
std::vector<T> to std::vector<T*> or std::vector<boost::shared_ptr<T>>
changes the semantics of the container quite dramatically.

Should profiling show that copy constructor and assignment calls for a class
are too expensive, one could reimplement the class so that it uses a copy
on write optimization internally without changing the public interface at
all. That would also reduce copy costs at other places in the program.
Best

Kai-Uwe Bux
Oct 19 '05 #9

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

Similar topics

6
2815
by: Amir Hardon | last post by:
I'm new to DOM and can't figure out this thing: I'm trying to add a row to a table with a form field in one of it's cells, but if I'm appending the field to a form it gets out of the table. Can...
34
4128
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
2
18546
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
2
4719
by: vsgdp | last post by:
From what I learned, if you want to do random element insertions and deletions you should use a list. But, with std::vector, if the order of the elements does not matter, couldn't you efficiently...
4
3701
by: glebur | last post by:
Hi, I'm trying to create a web service client in C# but I get stuck at one of the first steps. When adding a Web reference to the Visual Studio project; I get this error (this is a translation,...
2
7522
by: Joe | last post by:
Anyone can suggest the best method of reading XML and adding data to ListView? Here is the xml data structure:: <xml> <site> <url>http://www.yahoo.com</url> <lastupdate></lastupdate>...
1
2448
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
14
3450
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src=""...
13
31611
by: jm.suresh | last post by:
It is not possible to index set objects. That is OK. But, what if I want to find some element from the Set. from sets import Set s = Set( range(12 ) if I do pop, that particular element gets...
0
7234
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
7344
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
7412
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
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4730
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...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.