vector subscript out of range
2009-11-24 14:25
393 查看
当初写日志,只是为了记录一下自己所做的事情,没想到会有别人来看。所以没有任何的解释,只是胡乱写了些东西。
其实问题很简单,题目已经说明一切,错误就是越界,仔细检查代码,找出什么地方可能会越界就行了
昨天运行程序时,突然出现了如题所述的错误。错误很明显,vector的下标越界了,但是之前一直没有什么问题啊,于是我仔细读代码,debug,然后拿出前天能运行的程序,依然会出现这个错误。
开始时不甘心,一直想着昨天之前都能运行,错误应该出在昨天的代码里面。
于是找啊找,找了一天终于还是没有找到。
今天继续找,不过今天已经不找昨天写的代码了,开始看看里面调用的之前的函数,结果很短时间就找出来了,一个很低级的手误。。。
总结:不能太相信自己,适当相信这些“标准”,毕竟是这么多人总结出来,被更多的人用,提示这个错误就先找找这种错误
要更加仔细
要学会debug,写完代码多运行几遍
另外,明白了vector里面的at & [],源码如下:
The difference between this member function and member operator function operator[] is that vector::at signals if the requested position is out of range by throwing an out_of_range exception.
operator [] signals only if _HAS_ITERATOR_DEBUGGING.虽然不是太明白,但是应该是at()会检查越界,而[]除了在这种debug时,是不会查越界的。所以我以后还是用.at()吧。不过 这样会降低效率,还是[]在实际中应该会好些。一是简单可读,二是只在调试的时候查越界,调试完使用时不检查,节约时间。
不过好像c++ 0x会两个都检查了。不清楚,等待新标准
其实问题很简单,题目已经说明一切,错误就是越界,仔细检查代码,找出什么地方可能会越界就行了
昨天运行程序时,突然出现了如题所述的错误。错误很明显,vector的下标越界了,但是之前一直没有什么问题啊,于是我仔细读代码,debug,然后拿出前天能运行的程序,依然会出现这个错误。
开始时不甘心,一直想着昨天之前都能运行,错误应该出在昨天的代码里面。
于是找啊找,找了一天终于还是没有找到。
今天继续找,不过今天已经不找昨天写的代码了,开始看看里面调用的之前的函数,结果很短时间就找出来了,一个很低级的手误。。。
总结:不能太相信自己,适当相信这些“标准”,毕竟是这么多人总结出来,被更多的人用,提示这个错误就先找找这种错误
要更加仔细
要学会debug,写完代码多运行几遍
另外,明白了vector里面的at & [],源码如下:
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 (*(begin() + _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_DEBUGGING */ _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 range by throwing an out_of_range exception.
operator [] signals only if _HAS_ITERATOR_DEBUGGING.虽然不是太明白,但是应该是at()会检查越界,而[]除了在这种debug时,是不会查越界的。所以我以后还是用.at()吧。不过 这样会降低效率,还是[]在实际中应该会好些。一是简单可读,二是只在调试的时候查越界,调试完使用时不检查,节约时间。
不过好像c++ 0x会两个都检查了。不清楚,等待新标准
相关文章推荐
- 对于windows平台下的vs2010下vector数组提示“vector subscript out of range”很头疼
- vector subscript out of range .
- vector subscript out of range
- 使用容器出现vector subscript out of range等类似错误
- 【C++】bug之vector subscript out of range
- vector subscript out of range问题解决方案之一
- vector subscript out of range(容器越界)
- 容器赋值 容器使用问题:vector subscript out of range
- 使用vector出现的错误 subscript out of range
- Opencv 报错:vector subscript out of range
- vector subscript out of range 第一篇
- vector subscript out of range
- C++学习之路(14)---vector报错:vector iterator not incrementable"或"vector iterator + offset out of range"
- 调bug:terminating with uncaught exception of type std::out_of_range: vector Abort trap: 6
- C++ 使用vector 出现"vector iterator not incrementable"或"vector iterator + offset out of range"错误的情况
- terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check
- c++ Expression: string subscript out of range
- C++泛型指针的正向与逆向循环读取时报错Expreeeion:vector iterator + offset out of range问题
- 使用PCL的IntegralImageNormalEstimation遇到的vector subsript out of range问题
- string subscript out of range