What does container invalidation in C++ mean? -


I have learned about the term invalidation in the context of C ++ Containers Can explain what that means?

It seems that while looping over the container, the elements of any container are not allowed to be modified.

Thanks, Boda Sido "itemproc =" text ">

Containers are not invalidated - Iterators that refer to container elements are invalid. / P>

An Iterator A special item is the Iterator valid within a container, as long as the item stays inside the container and the container does not rearrange itself internally, when one of two things happens , Then an Iterator becomes invalid, because the left The medicinal Iterator is no longer valid as a handle in the container.

The most obvious way to invalidate an erratator is by removing its referenced item from the collection, example:

 < Code> std :: set & lt; int> s.insert (4); s.insert (2); std :: set & lt; int & gt; :: iter itr = s.find (4 ); // IRR 4 Stud :: COAT and LT;   

A more subtle way of canceling an iterator is the reason for internally rearranging the container (such as reassigning its internal storage) For example, to expand some types of containers Due to this, it can be:

  std :: vector & lt; Int & gt; V; V.push_back (4); V.push_back (2); Std :: vector & lt; Int & gt; :: Iterator itr = v.begin (); // IRR4 Stud :: COAT and LT; & Lt; * IRR & lt; & Lt; Std :: endl; // print 4 v.push_back (12); // increases your internal allocation, then  

you can pause it in some containers by a pre-booked location:

  std :: vector & lt ; Integer & gt; V; V.reserve (3); // Pre-allocated 3 elements v.push_back (4); V.push_back (2); Std :: vector & lt; Int & gt; :: Iterator itr = v.begin (); // IRR4 Stud :: COAT and LT; & Lt; * IRR & lt; & Lt; Std :: endl; // print 4 v.push_back (12); // will not cancel the IRR, because it will never cause the extension to  

. Each STL container should have a description of the document, in which case the Iterator can be invalidated or may be.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -