Vector iterators Incompatible

Source: Internet
Author: User

Literal translation iterator type is incompatible

The problem that my colleague met today is a question of habitual wording. Describe the code:

struct track{};

Class Basetrack

{

...

Std::vector<track> gettracksourcelist ();

...

};

The problem lies in:

void func (Basetrack bt)

Std::vector<track>::iterator it = BT. Gettracksourcelist (). Begin ();

for (; it! = Bt. Gettracksourcelist.end (); ++it)

Focus is the red part, the value is not a reference, no get once is a stack of space, can not be compared. But the errors reported here (Iterators are incompatible) are not very understandable.

There are many articles on the Internet, although not the cause of the problem today, but also can be very good for reference.

To intercept parts of which are of reference value:

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;

}

}

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"

Workaround: Change to a pass-by reference. Or use an array to store the get information , and then set the array back to the end of the operation.

There are also many errors caused by invalid iterators when erase is removed, workaround: Use the return value of erase to record the next position of the iterator.

Reference Address:

http://blog.csdn.net/olanmomo/article/details/38420907

Http://stackoverflow.com/questions/8421623/vector-iterators-incompatible

Vector iterators Incompatible

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.