473,385 Members | 2,069 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,385 software developers and data experts.

Inserting an empty element in a list (STL)

How can I insert an empty element in a list? The insert method has as
a parameter the source object to insert... So you have to use it in
this way:
(where MyObject is an object with only a member, x (an integer))
Normally a copy constructor is longer to execute than a simple
constructor, and this example code uses the constructor once (for the
obj object) and the copy constructor five times (to add the elements
in the list)

list <MyObject>::iterator L_Iter;
list<MyObject> L;
MyObject obj;

for (int i = 1 ; i < 6 ; i++ )
{
L_Iter = L.insert(L.end(), obj);
L_Iter->x = i;
}
Jul 19 '05 #1
3 2168
WW
Massimiliano Alberti wrote:
How can I insert an empty element in a list? The insert method has as
a parameter the source object to insert... So you have to use it in
this way:
(where MyObject is an object with only a member, x (an integer))
Normally a copy constructor is longer to execute than a simple
constructor, and this example code uses the constructor once (for the
obj object) and the copy constructor five times (to add the elements
in the list)

list <MyObject>::iterator L_Iter;
list<MyObject> L;
MyObject obj;

for (int i = 1 ; i < 6 ; i++ )
{
L_Iter = L.insert(L.end(), obj);
L_Iter->x = i;
}


This is how STL works. Annoying, but this is the way. Some of us were
porposing to look at possible templated solution (so it would try to use
your int to construct the "object inside") but the Goths (Gurus Of The Holy
Standard) said that its stupid, it is only our problem noone else has it
etc. At that time I was receiving 5 mails per day saying: I have that
problem too...

Thanx God in your case it does not matter. If obj has an inlinable, simple
constructor it does not matter if you pass in an int or an obj. What it
will end up being is copying the int.

--
WW aka Attila
Jul 19 '05 #2
> Thanx God in your case it does not matter. If obj has an inlinable, simple
constructor it does not matter if you pass in an int or an obj. What it
will end up being is copying the int.


I thought it was quite obvious that it was only a sample... and then
you can't know what are the other members of that class... You know
only what I've initialized... Perhaps there is a char buffer[10000]
:-)
Jul 19 '05 #3
WW
Massimiliano Alberti wrote:
Thanx God in your case it does not matter. If obj has an inlinable,
simple constructor it does not matter if you pass in an int or an
obj. What it will end up being is copying the int.


I thought it was quite obvious that it was only a sample... and then
you can't know what are the other members of that class... You know
only what I've initialized... Perhaps there is a char buffer[10000]
:-)


I know it is not true. You have a long double unused[10000] ;-)

--
WW aka Attila
Jul 19 '05 #4

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

Similar topics

16
by: bill | last post by:
How can I insert a record with a null value in one of the fields? I need to use the update method from a data adapter. The data is in a XML file.
25
by: Matthias | last post by:
Hi, I am just reading that book by Scott Meyers. In Item 4 Meyers suggests to always use empty() instead of size() when probing for emptyness of STL containers. His reasoning is that size()...
10
by: Ryan Graham | last post by:
I totally bombed this question in an interview so I'm posting my answer here for comments and suggestions... perhaps (god help me) I'm just not that bright, but this works and seems to be fairly...
6
by: Allerdyce.John | last post by:
Hi, How can I Random access an element in a STL List if I have a pointer to that list? I know how to do that if I have a reference to a STL list, but how can I do that if I have a pointer to a...
59
by: Steve R. Hastings | last post by:
So, Python 2.5 will have new any() and all() functions. http://www.python.org/dev/peps/pep-0356/ any(seq) returns True if any value in seq evaluates true, False otherwise. all(seq) returns...
6
by: Jakob Bieling | last post by:
Hi, I want to move an element from a std::list to the end of the same list. To get this done, I thought I'd just do something like: std::list <intlst; lst.push_back (0); lst.push_back (1);...
2
by: gruvin | last post by:
Hi. I need to insert an image element into text in an iframe in design mode. That part I can do: var sel = oEditor.eFrameDoc.selection; var range = sel.createRange(); range.pasteHTML('<img...
2
by: barcaroller | last post by:
If an STL container is empty, what will its begin() member function return? Would it be a valid value that an iterator could use, like the end() member function? In particular, can I assume that...
0
by: toyin | last post by:
hello, pls help look through this code its not inserting the record in dataset into the another database. i want to insert the row in the dataset into another table in another database. pls help....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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...

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.