A summary of vector problems

Source: Internet
Author: User

1.vector iterators Incompatible

Find the code that raised the error as follows:
for (Vectortype::iterator it = Somevector.begin ();; it!= somevector.end (); ++it;)
{
if (*it== value)
{
Somevector.erase (IT);
}
}
Code, after the erase operation, the loop continues without modifying it, and the assertion appears when compared to end ().
The main problem here is that vector can implement erase in any way, not guaranteeing that after an element is erase, subsequent elements must be moved to the location (address) referenced by this iterator. Of course, this is true in almost all implementations of the STL, which is why the previous run with VC6 has no problem. But if this is not a vector, but a list or a map, the program will collapse without hesitation.
The right thing to do is this:
The erase implementation of all container classes in the STL returns a iterator that points to the successor element of the currently deleted element, or end ().
Therefore, the return value of the erase is assigned to the iteration variable after the erase is removed from all elements of the container by deleting an element:
for (Vectortype::iterator it = Somevector.begin ();; it!= somevector.end ();)
{

if (*it== value)
{
it = somevector.erase (it);
}
Else
{
++it;
}
}
P.S. You can see that the VS2005 with the STL added more debugging features to avoid some of the STL error, conditional words best use VS2005 stl. If you don't have VS2005, you can also use Stlport,stlport to be very good at debugging features.

If you skip this check, you can use #define _has_iterator_debugging 0 but it is recommended that you check the synchronization code for the thread.


2. Space vector problem. The begin iterator of an empty vector is not allowed because the vector is empty and naturally there will be no first project, and the use will also cause vector iterators incompatible.

3.vector both read and write thrown. Vc2005 is very strict for iterators, usually because two different iterators operate the same vector, or because the vector's linked list changes when the iterator traverses the vector, such as when the vector is on the way to traverse it, The other position push_back () an element, at which point the iterator is invalidated, causing an error. You can use a critical section for mutually exclusive access.

4. If the iterator is out of bounds, an illegal parameter handler will be invoked accordingly.
Again, you can avoid illegal parameter problems by throwing a cross boundary exception. Add #define value of _secure_scl_throws to the code and set the value to 1 so that no illegal parameter handlers are invoked, but an exception is generated.
You can also turn off this iterator check by setting the value of #defined value of _SECURE_SCL to zero, which is typically turned on by default.

5.VS2005 compiles the release version of the STL iterator bug. The test of the Unicode version of the comparison is easy to do this problem, is a baffling iterator error.
Microsoft's original: The bug afflicted all Standard containers (vectors, deque, list, set, Multiset, map, Multimap, and string) when _has_ Iterator_debugging was disabled and _SECURE_SCL is enabled. Additionally, Deque is afflicted when both were disabled. The solution is

#ifndef _DEBUG

#define _SECURE_SCL 0

#endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.