vector.at(n) vs vector[n]

This is a discussion on vector.at(n) vs vector[n] within the C++ Programming forums, part of the General Programming Boards category; Hi, I have just read that at() is checked access, whereby [] is unchecked. However, when I try to access ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    vector.at(n) vs vector[n]

    Hi,

    I have just read that at() is checked access, whereby [] is unchecked. However, when I try to access out of range elements using either approach I get an error message on both , albeit different ones.

    Is it the choice of the IDE to range check []?

    Thanks.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,764
    at member function throws an exception if the index is out of range. The index operator does not.
    Now, if an implementer chooses to throw asserts in debug mode with either, that's their prerogative. The standard doesn't care, I think. Though it would care if the index operator started throwing exceptions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    I thought that maybe the case. What would you say is better practise then? Using subscripting or the at member function?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,764
    at, unless you are in dire need of speed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Okay, thanks.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21