Several possibilities of the "vector iterators incompatible" error in the iterator report (reproduced)

Source: Internet
Author: User

Summary: There are vectors and other members in the structure, do not use Memset 0 (from the tracking of the library I found that the vector is using the name "_myproxy", "_mynextiter" the two pointers to find the value adjacent to it, when we define a vector, it initializes a "_ Myproxy ", and My code performs a zero-zeroing operation on the defined struct in the class's constructor )

Because the usual iterators and vectors are not many, so this morning encountered a very strange problem, wasting my whole 4 hours! Distressed Ah!! here is the detailed pass, do not want to see the past can be skipped directly to see my summary. my project needs to deal with very complex data structures, and there are many structures that are self-growing, so if you want to properly manage and process this data there will be more than 10 different dynamic array support, in order to make the code more concise, it is intended to use vectors instead of dynamic data. Unexpectedly is this seemingly reasonable choice but brought me trouble, in my debugging to the most complex code place, just as my brain frequency and brain cache are unusually tight, suddenly pop up a let me not touch the wrong head, the brain directly down ...

This error is "vector iterators incompatible", prompting me that vectors are incompatible with iterators, but this is obviously impossible, and my code at the time was roughly the following:

--------------------------------------------------------------------------------

....

....

typdef struct _STRUCT

{

....

....

Vector<info> Vecinfo;

}struct;

STRUCT stcstruct;

....

....

For (Vector<info>::iterator i= stcStruct.vecInfo.begin ();

I! = StcStruct.vecInfo.end ();

i++)//Error here

{

..... www.2cto.com

....

}

--------------------------------------------------------------------------------

By the code, I do not use any other type of vector, only the type of info, how can this error, baffled it, follow-up library code for the moment there is no harvest.

There is no way, like Google uncle to help it, the online gave the following several will cause this error situation, although useless to me, but still sorted out, convenient for you to check:

1, type mismatch, for example, with the int type vector iterator and the char type vector iterator for the comparison operation.

2, the comparison of the vector structure changes, such as the following code:

--------------------------------------------------------------------------------

For (Vector<int>::iterator i= vector.begin ();

I! = Vector.end ();

i++)//Error here

{

Vector.erase (i);

....

}

--------------------------------------------------------------------------------

After the erase operation, the loop variable i is not pointed to the modified vector iterator, it continues the loop, and the assertion appears when compared to end ().

The solution is to "vector.erase (i);" Replace with "i = vector.erase (i);", because the erase implementation in all container classes in the STL will return an iterator that points to "the successor of the currently deleted element, or end ()".

In addition to the above two, there is no other explanation, but after another attempt to follow up the library code, in the vector class is not equal to the overloaded with the following code, and then found the mystery ...

--------------------------------------------------------------------------------

void _compat (const _myiter& _right) const

{//test for compatible iterator pair

if (this->_getcont () = = 0//Determine whether _myproxy is 0, 0 is an error, otherwise get the owning container

|| This->_getcont ()! = _right._getcont ())//Judging whether the type class of two vectors is consistent

{//Report error

_debug_error ("Vector iterators incompatible");

_scl_secure_invalid_argument;

}

}

--------------------------------------------------------------------------------

We are following up on _getcont (), _getcont () code as follows:

--------------------------------------------------------------------------------

Const _CONTAINER_BASE12 *_getcont () const

{//Get owning container

return (_myproxy = = 0? 0: _myproxy->_mycont);

}

--------------------------------------------------------------------------------

Here I find that the _myproxy in my code is 0, which means that our type should be no problem, but the "chain" of the vector is broken.

From the tracking of the library, I find that the vectors are using two pointers named "_myproxy" and "_mynextiter" to find the adjacent values, and when we define a vector, it initializes a "_myproxy", My Code, however, performs a zero-zeroing operation on the defined struct in the class's constructor:

ZeroMemory (&m_stcstruct, sizeof (STRUCT));

Thus causing the "_myproxy" in the vector is lost, although it can still be push_back () and other almost everything else, but this vector is not the only one can not perform a walker operation, how obscure a mistake ah ...

Summary, write code when you do not forget to initialize their own construction of the thing is a good habit, but also pay attention to not impostors, know how to operate anything can, if it is not understand things, or first understand for good.

Always keep an awe-inspiring mind on the computer in front of you.

Several possibilities of the "vector iterators incompatible" error in the iterator report (reproduced)

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.