Use out_of_range exception instead of manual bounds checking
Const-correctness and minor clean-ups