From 850f2d1e58cc443f77353c7fc0ce0c158c1fd328 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Sun, 26 Oct 2014 16:48:44 +0100 Subject: [PATCH] Add the at() method to C++ vectors. * There is no reason not to have this. --- headers/cpp/stl_vector.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/headers/cpp/stl_vector.h b/headers/cpp/stl_vector.h index d1149e9af6..848a5fa4df 100644 --- a/headers/cpp/stl_vector.h +++ b/headers/cpp/stl_vector.h @@ -31,6 +31,8 @@ #ifndef __SGI_STL_INTERNAL_VECTOR_H #define __SGI_STL_INTERNAL_VECTOR_H +#include + __STL_BEGIN_NAMESPACE #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) @@ -218,6 +220,18 @@ public: reference operator[](size_type __n) { return *(begin() + __n); } const_reference operator[](size_type __n) const { return *(begin() + __n); } + reference at(size_type __n) { + if (__n >= size()) + throw std::out_of_range("vector::at() out_of_range"); + return *(begin() + __n); + } + + const_reference at(size_type __n) const { + if (__n >= size()) + throw std::out_of_range("vector::at() out_of_range"); + return *(begin() + __n); + } + explicit vector(const allocator_type& __a = allocator_type()) : _Base(__a) {}