vector : accessing values

This is a discussion on vector : accessing values within the C++ Programming forums, part of the General Programming Boards category; eg: vector<int> listOfValues Hi, Vectors can be accessed by index like listOfVaules[9] OR by iterator. which is the best way ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    vector : accessing values

    eg: vector<int> listOfValues

    Hi,
    Vectors can be accessed by index like listOfVaules[9]
    OR
    by iterator.

    which is the best way of accessing? is accessing by index a bad practice?

    Thanks,
    Rahul

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    No, it is not bad practice.

    Better practice might be to use at() instead of operator[] with an index to help avoid using invalid indexes. You should be fine with any of those options, though.

    Note that depending on the situation, using an index or using an iterator might be more clear to a reader of the code than the other option. Often that's the most important factor in your choice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. Replies: 1
    Last Post: 12-30-2007, 09:08 AM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  4. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM

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