473,725 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invalidating iterators after insertion/removal


If I insert/remove an element in a set, will an iterator to this set
automatically become invalid? Does the position of the iterator before the
insertion/removal matter?

How are vectors and lists affected?
Jun 6 '07 #1
6 2319
"barcarolle r" <ba*********@mu sic.netwrote in
news:f4******** **@aioe.org:
>
If I insert/remove an element in a set, will an iterator to this set
automatically become invalid? Does the position of the iterator
before the insertion/removal matter?
Insertion doesn't invalidate iterators into a set, deletion will invalidate
iterators (and pointers and references) to that element only.
How are vectors and lists affected?
Lists have the same rules as set. For vectors, insertion may invalidate
_all_ iterators into the vector if the insertion requires a reallocation.
(Hadn't thought about it, but it would invalidate all iterators at the
point of insertion and beyond in all cases. Then again, vectors are most
frequently added to at the end of the container, so there's normally
nothing to invalidate.) Deletion from a vector invalidates that iterator
and all iterators past it.
Jun 6 '07 #2
Andre Kostur wrote:
"barcarolle r" <ba*********@mu sic.netwrote in
news:f4******** **@aioe.org:
>If I insert/remove an element in a set, will an iterator to this set
automaticall y become invalid? Does the position of the iterator
before the insertion/removal matter?

Insertion doesn't invalidate iterators into a set, deletion will invalidate
iterators (and pointers and references) to that element only.
>How are vectors and lists affected?

Lists have the same rules as set. For vectors, insertion may invalidate
_all_ iterators into the vector if the insertion requires a reallocation.
(Hadn't thought about it, but it would invalidate all iterators at the
point of insertion and beyond in all cases. Then again, vectors are most
frequently added to at the end of the container, so there's normally
nothing to invalidate.) Deletion from a vector invalidates that iterator
and all iterators past it.
How is a vector implemented? I know that set and map are just
abstractions of a red-black tree, but what about vectors?
Jun 7 '07 #3
On 7 Juni, 09:58, desktop <f...@sss.comwr ote:
Andre Kostur wrote:
"barcarolle r" <barcarol...@mu sic.netwrote in
news:f4******** **@aioe.org:
If I insert/remove an element in a set, will an iterator to this set
automatically become invalid? Does the position of the iterator
before the insertion/removal matter?
Insertion doesn't invalidate iterators into a set, deletion will invalidate
iterators (and pointers and references) to that element only.
How are vectors and lists affected?
Lists have the same rules as set. For vectors, insertion may invalidate
_all_ iterators into the vector if the insertion requires a reallocation.
(Hadn't thought about it, but it would invalidate all iterators at the
point of insertion and beyond in all cases. Then again, vectors are most
frequently added to at the end of the container, so there's normally
nothing to invalidate.) Deletion from a vector invalidates that iterator
and all iterators past it.

How is a vector implemented? I know that set and map are just
abstractions of a red-black tree, but what about vectors?
Since all elements must be stored in contiguous memory it is usually
implemented with an array.

--
Erik Wikström

Jun 7 '07 #4
Erik Wikström wrote:
Since all elements must be stored in contiguous memory it is usually
implemented with an array.
Usually? You mean there are other ways?
Jun 7 '07 #5
On 7 Juni, 12:59, Juha Nieminen <nos...@thanks. invalidwrote:
Erik Wikström wrote:
Since all elements must be stored in contiguous memory it is usually
implemented with an array.

Usually? You mean there are other ways?
Well, it's implementation dependent and there might be some
implementer that does something else (perhaps using malloc to reserve
a piece of memory and then use placement new or something). The point
is that it is not defined in the standard so it can be done however
you wish as long as it's compliant, however an array is probably the
easiest and best way to do it.

--
Erik Wikström

Jun 7 '07 #6
On Jun 7, 2:07 pm, Erik Wikström <eri...@student .chalmers.sewro te:
On 7 Juni, 12:59, Juha Nieminen <nos...@thanks. invalidwrote:
Erik Wikström wrote:
Since all elements must be stored in contiguous memory it is usually
implemented with an array.
Usually? You mean there are other ways?
Well, it's implementation dependent and there might be some
implementer that does something else (perhaps using malloc to reserve
a piece of memory and then use placement new or something). The point
is that it is not defined in the standard so it can be done however
you wish as long as it's compliant, however an array is probably the
easiest and best way to do it.
The standard requires that the memory be contiguous, i.e. that
&vect[i]+1 == &vect[i+1], for all valid i. Which pretty much
limits the choices in the implementation. The standard also has
pretty strict requirements concerning when constructors of
vector elements are called, and when iterators, pointers and
references are invalidated, which ultimately more or less
require that allocation and initialization be separated, i.e.
that placement new be used for construction. The standard also
requires that all dynamic memory used be allocated by the global
operator new() function (at least when the default allocator is
used). Given all this, an implementation really doesn't have
much freedom. A typical implementation will use three pointers:
one to the start, one to the end of the initialized values, and
one to the end of the raw memory---it may replace the latter two
with integral values, and it may add extra data for error
checking (i.e. a list of active iterators), but that's about it.

Note that vector<Tcannot be implemented using new T[]. It
must separate allocation and initialization.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 8 '07 #7

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

Similar topics

9
2549
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences, but try to use an iterator of a list that you're mutating and watch it all fall to pieces. That is, if you change the length of a section of the list through which the iterator has already passed, it will lose track of where it is. I think...
0
1386
by: Viper99 | last post by:
Need to detect (receive notification) for the insertion/removal of PCMCIA devices on Windows 2000/XP in a C# .Net application. Was able to do this in ATL COM app using CWnd::OnDeviceChange().
1
1773
by: bartek | last post by:
Hi, I'm sorry for asking such a simple question. I just would like to know what's the standard-defined behaviour when doing a push_back() on a std::deque. TIA, b
19
7540
by: John | last post by:
In STL's map implementation, the distance between two iterators it1, it2 takes O(K) to compute where K is the actual distance between the two iterators. In theory, a red black tree can do this in O(log K) time. Anyone knows if there is a way to do this in map<> or if map<> can be inherited/modified to do this? Thanks, --j
13
1923
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize this? I wrote the following program to test this out. #include <fstream> #include <iostream> #include <string>
0
1403
by: subramanian100in | last post by:
Suppose I have vector<intc; for (int i = 0; i < 10; ++i) c.push_back(i); vector<int>::iterator it = find(c.begin(), c.end(), 5); If I do, c.insert(c.begin(), 10);
6
2988
by: elizabeth1986 | last post by:
Hello, Is it possible to detect the insertion and removal of Plug and play devices (for example : keyboard , mouse etc) while the computer is still ON. We wanted to do the above in C#. We tried a couple of WMI classes like win32_Keyboard class, but after manually removing the Keyboard, there was no change in the Property "status" of win32_Keyboard. it showed that the status was OK. We also tried a...
2
2410
by: subramanian100in | last post by:
In ISO/IEC 14882:2003 document, in the section '23.2.1.3 deque modifiers', the following is mentioned: iterator insert(iterator position, const T& x); void insert(iterator position, size_type n, const T& x); template <class InputIterator> void insert(iterator position, InputIterator first, InputIterator last);
5
2223
by: Leon | last post by:
using multimap<int,int>::iterator itlow = pq.lower_bound (x); multimap<int,int>::iterator itup = pq.upper_bound (y); I obtain lower and upper bound from the multimap, and having these two iterators I would like to sample one element with uniform distribution. It a way do to this using iterators? I can of course draw an integer and loop over the sequence until I meet the drawn value, but it is not a very nice solution. Can sample...
0
9401
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9179
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
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 we have to send another system
3
2157
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.