CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2008
    Posts
    78

    For-loop: Vector Iterator vs. Vector.at() or []

    Supposing I want to access a vector's data sequentially using a for-loop, should I use an iterator or can I simply access elements with at() or []?

    I want to know about the advantages or disadvantages.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: For-loop: Vector Iterator vs. Vector.at() or []

    In the case of a vector, I'm not sure it makes much difference.

    The advantage of using an iterator is that the same code would work if you later decided to swap in, say, a set instead.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: For-loop: Vector Iterator vs. Vector.at() or []

    1) As mentioned above, using iterators increases the ability
    to write generic functions. Also, in debug mode, some
    iterators have validity checks in them.

    2) Operator [] tends to be more readable since the code looks
    like code using simple c-style arrays.

    3) at() will throw an exception if the index is out of range.

  4. #4
    Join Date
    Jan 2008
    Posts
    78

    Re: For-loop: Vector Iterator vs. Vector.at() or []

    Thank you. That elucidates me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured