vector::at vs. vector::operator[]

前端 未结 8 1816
走了就别回头了
走了就别回头了 2020-11-30 18:37

I know that at() is slower than [] because of its boundary checking, which is also discussed in similar questions like C++ Vector at/[] operator sp

8条回答
  •  清歌不尽
    2020-11-30 19:13

    at can be clearer if you have a pointer to the vector:

    return pVector->at(n);
    return (*pVector)[n];
    return pVector->operator[](n);
    

    Performance aside, the first of these is the simpler and clearer code.

提交回复
热议问题