Disabling bounds checking for c++ vectors

前端 未结 8 2424
囚心锁ツ
囚心锁ツ 2021-02-08 06:09

With stl::vector:

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

Is there a way to disable bounds checking

8条回答
  •  难免孤独
    2021-02-08 06:38

    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 file.

提交回复
热议问题