473,554 Members | 3,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::ite rator 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 2189
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>::ite rator 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
1837
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
7531
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() might take linear time on some list implementations. That makes sense at first. However, he also says this at the very beginning: "That being the...
10
11584
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 efficent. The idea was simple, insert an integer into a list that has already been sorted. private int _list; .... public void Insert(int value)...
6
6875
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 STL List. void function (vector<int>& myLIst) { // access the first element of myList printf ("%d", myList);
59
8746
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 True if all values in seq evaluate true, False otherwise. I have a question: what should these functions return when seq is an empty list?
6
2765
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); lst.push_back (2); lst.splice (lst.end (), lst, lst.begin (), ++ lst.begin ());
2
3441
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 src="'+imagePath+'" border="0" />'); But I need a reference to that new IMG tag to do other stuff with it.
2
4844
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 begin() == end()?
0
2225
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. it urgent i submit this application. when it runs it gives: this OdbcTransaction has completed,it no longer usable. thx toyin here is the code ...
0
7507
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7783
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8019
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6127
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5424
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5143
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1115
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.