Possible errors reported by the iterator "vector iterators incompatible"

Source: Internet
Author: User

 

Since not many iterators and vectors are used at ordinary times, this morning I encountered a very strange problem and wasted 4 hours! Distressed !!

The following is a detailed summary.

 

 

My project needs to process a very complex data structure, and many of the structures are self-increasing. Therefore, if we want to properly manage and process the data, we need to support a dozen different dynamic arrays, to make the code more concise, we intend to use vectors instead of dynamic data.

 

 

I did not expect that this seemingly reasonable choice has brought me troubles. When I debug the code to the most complex place, while the frequency of my brain and the cache are exceptionally tight, suddenly, a mistake that made me unable to touch my mind popped up. The brain went down directly ......

 

 

This error is "vector iterators incompatible", prompting that the vector is incompatible with the iterator, but this is obviously impossible. My code at that time was roughly as follows:

 

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

 

....

....

Typdef struct _ STRUCT

{

....

....

Vector <INFO> vecInfo;

} STRUCT;

 

 

STRUCT stcStruct;

....

....

For (vector <INFO >:: iterator I = stcStruct. vecInfo. begin ();

I! = StcStruct. vecInfo. end ();

I ++) // an error is reported here.

{

... Www.2cto.com

....

}

 

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

 

The Code shows that I didn't use any other types of vectors, and I only have INFO. How can I make this error? I have no idea about it, and I have nothing to do with the code that comes into the database.

No way. Ask Uncle Google for help. I have provided the following information on the Internet that will cause this error. Although it is useless to me, I have sorted it out for your convenience:

1. Type Mismatch. For example, the int Type Vector iterator is used for comparison with the char type vector iterator.

2. The structure of the vector changes during comparison, for example, the following code:

 

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

 

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

I! = Vector. end ();

I ++) // an error is reported here.

{

Vector. erase (I );

....

}

 

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

 

After the erase operation, the loop variable I is not directed to the modified vector iterator, And the loop continues. When it is compared with end (), the assertions appear.

The solution is to convert "vector. replace erase (I); "with" I = vector. erase (I); ". This is because the erase implementation in all the container classes in STL returns an iterator pointing to" the successor element of the currently deleted element, or end () ".

In addition to the above two, there is no other explanation, but then I tried again with the library code, followed by the following code in the unequal number overload of the Vector class, and then found the xuanjicang ......

 

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

 

Void _ Compat (const _ Myiter & _ Right) const

{// Test for compatible iterator pair

If (this-> _ Getcont () = 0 // judge whether _ Myproxy is 0. if it is 0, an error is returned. Otherwise, obtain the container

| This-> _ Getcont ()! = _ Right. _ Getcont () // determines whether the type classes of the two vectors are consistent.

{// Report error

_ DEBUG_ERROR ("vector iterators incompatible ");

_ SCL_SECURE_INVALID_ARGUMENT;

}

}

 

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

 

We are following up with the _ Getcont () and _ Getcont () Code as follows:

 

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

 

Const _ Container_base12 * _ Getcont () const

{// Get owning container

Return (_ Myproxy = 0? 0: _ Myproxy-> _ Mycont );

}

 

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

 

Here I found that _ Myproxy in my code is 0, which means that our type should not be a problem, but the "chain" of the vector is broken.

From the tracking of the library, I found that the vector uses the pointers "_ Myproxy" and "_ Mynextiter" to find the adjacent values. When we define a vector, it initializes a "_ Myproxy", while my code executes a clearance operation on the struct defined in the class constructor:

ZeroMemory (& m_stcStruct, sizeof (STRUCT ));

 

 

As a result, the "_ Myproxy" in the vector is lost. Although it can still perform push_back () and other operations on it, this vector is missing, but it cannot be used to perform traversal, what a concealed mistake ......

 

 

To sum up, it is a good habit to write code without forgetting to initialize the self-constructed things, but you should also be careful not to overwrite the code and understand how to operate things at will. If you do not understand things, it is better to know about it first.

Always be in awe of your computer ......

From: A1Pass

Related Article

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.