2011-04-08 39 views

回答

56

您可以通过编译 - D_GLIBCXX_DEBUG来激活运行时迭代器和边界检查。另请注意,随机访问容器除了提供operator []之外,还提供始终检查at()-操作。

参考文献:

GCC STL调试模式: http://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode_using.html#debug_mode.using.mode

在()操作:std::vector::at()std::deque::at()std::array::at()

+0

您是否可以指向文档?谢谢。 – pic11 2011-04-08 12:49:25

+0

http://www.cppreference.com/wiki/container/vector/at – 2011-04-08 13:00:18

+0

@ pic11:done; @JBJ谢谢:) – 2011-04-08 13:27:49

1

你应该重载operator[]您的特定类。如果要使用现有的STL容器,at()函数是operator[].的边界检查版本