Vector Subscript out of range

Source: Internet
Author: User

The original diary, just to record what they do, did not expect someone else to see. So there's no explanation, just scribbling something.


in fact, the problem is very simple, the title has been explained, the error is to cross the line, carefully examine the code, find out what might be out of bounds.


When I ran the program yesterday, I suddenly appeared the wrong as described in the title. The error is obvious, the vector's subscript is out of bounds, but there has been no problem, so I read the code carefully, debug, and then come up with a program that can run the day before yesterday, still have this error.

The beginning of the unwilling, always think of yesterday can run, the error should be out of yesterday's code inside.

So looking for, looking for a day finally still did not find.

Today continue to find, but today has not found yesterday to write the code, and began to look inside the call before the function, the result is very short time to find out, a very low-level hand mistake ...

Summary: Can not too believe in themselves, appropriate to believe that these "standards", after all, is so many people summed up, by more people use, hint this mistake first look for such a mistake

To be more careful

To learn debug, write code and run it more than once

In addition, understand the vector inside the AT & [], the source code is as follows:


Const_reference at (size_type _pos) const {//subscript nonmutable sequence with checking if (size () <= _pos) _xran (); Return (* (Begin () + _pos)); } Reference at (Size_type _pos) {//subscript mutable sequence with checking if (size () <= _pos) _xran (), Return (* (Beg In () + _pos)); } const_reference operator[] (size_type _pos) const {//Subscript nonmutable sequence #if _has_iterator_debugging if (size () <= _pos) {_debug_error ("vector Subscript out of range") _scl_secure_out_of_range;} #endif/* _has_iterator_debugg ING */_scl_secure_validate_range (_pos < Size ()); Return (* (_myfirst + _pos)); } reference operator[] (Size_type _pos) {//subscript mutable sequence #if _has_iterator_debugging if (size () <= _pos) {_debug_error ("vector Subscript out of range"); _scl_secure_out_of_range;} #endif/* _has_iterator_debugging * * _scl_secure_validate_range (_pos < Size ()); Return (* (_myfirst + _pos)); }  the difference between this member function and member operator function  Operator[] is that  Vector::at signals If the requested position is out of a range by throwing a   out_of_range exception.  operator [] signals only if _has_iterator_debugging. Although not too clear, but should be at () will check the bounds, and [] It's not going to go out of bounds except in this debug. So I'm going to use. at (). However, this will reduce efficiency, or [] in practice should be better. One is simple to read, and the other is only in the debugging time to check the boundaries, commissioning the end of the use of not checking, save time. But it looks like C + + 0x will both be checked. Not clear, waiting for the new standard

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.