禁用c ++向量的边界检查 [英] Disabling bounds checking for c++ vectors

查看:113
本文介绍了禁用c ++向量的边界检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用stl :: vector:

 矢量< int> v(1); 
v [0] = 1; //无边界检查
v.at(0)= 1; //绑定检查

有没有办法禁用边界检查而不必重写所有 at() as []

:我改变了 at() [] 在我怀疑瓶颈的地方,它大大减少了计算时间。然而,由于我在开发代码和运行实验之间进行迭代,我想在开发期间启用边界检查,并在我真正运行实验时禁用它。我想安德鲁的建议是最好的解决方案。

解决方案

如果你真的想这样做比较),如果您在()

 <$ c $没有其他 c> #define at(x)operator [](x)

code> at()用于开发,并在生产中使用 operator [] ifdef 。



如果你有其他的()编辑 #include d < vector> 文件。


With stl::vector:

vector<int> v(1);
v[0]=1; // No bounds checking
v.at(0)=1; // Bounds checking

Is there a way to disable bounds checking without having to rewrite all at() as []? I am using the GNU Standard C++ Library.

Edit: I changed at() to [] in the area where I suspected a bottleneck, and it significantly reduced the computation time. However, since I iterate between developing the code and running experiments with it, I would like to enable bounds checking during development and disable it when I run the experiments for real. I guess Andrew's advice is the best solution.

解决方案

If you really want to do it (at least for a quick and dirty profiling comparison), this will work if you have no other at()s

#define at(x) operator[](x)

And if you want to keep at() for development and use operator[] in production, just wrap it in an #ifdef.

And if you do have other at()s you can always edit your #included <vector> file.

这篇关于禁用c ++向量的边界检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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