是矢量的范围比堆阵列检查出更严格? [英] Are vectors more rigorous at checking out of bounds than heap arrays?

查看:110
本文介绍了是矢量的范围比堆阵列检查出更严格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何严格的界限上向量检查相比,堆阵列?它究竟是如何检查边界和如何与一个堆数组如何检查比较?

How rigorous is the bounds checking on vectors compared to heap arrays? How exactly is it checking bounds and how does that compare with how a heap array is checked?

推荐答案

A 矢量会做边界检查,如果你使用在()的功能,例如:

A vector will do bounds checking if you use the at() function, for example:

std::vector<int> v(5);
v.at(3) = 10; 
v.at(5) = 20; // throws an exception, std::out_of_range

不过,如果你使用运算符[] ,没有边界检查。 (和访问不存在的元素会导致未定义的行为。)

However, if you use operator[], there is no bounds checking. (And accessing non-existent elements leads to undefined behavior.)

<子>应当指出,虽然,大多数实现将有可能以包括边界检查在所有迭代器,这是在回答中讨论<一href=\"http://stackoverflow.com/questions/1290396/how-to-make-stdvectors-operator-compile-doing-bounds-checking-in-debug-but-n\">here.默认情况下,VS2008及以下有它的调试和发布,仅VS2010确实在调试。 gcc,需要定义 _GLIBCXX_DEBUG 来得到检查的迭代器。

It should be noted, though, that most implementations will have the possibility to include bounds-checking on all iterators, which is discussed in the answers here. By default, VS2008 and below have it on in Debug and Release, VS2010 does only in Debug. gcc requires you define _GLIBCXX_DEBUG to get checked iterators.

这篇关于是矢量的范围比堆阵列检查出更严格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆