std::vector iterators

This is a discussion on std::vector iterators within the C++ Programming forums, part of the General Programming Boards category; For Code: std::vector<type> v(/*not empty*/); Is Code: v.begin() + v.size() == v.end() always true? Is end() literally one-past-last?...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917

    std::vector iterators

    For
    Code:
    std::vector<type> v(/*not empty*/);
    Is
    Code:
    v.begin() + v.size() == v.end()
    always true? Is end() literally one-past-last?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    .end() is always literally one-past-last. And since vector's iterators can be added to, then v.begin() + v.size() must always equal v.end().

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Even if a vector is empty it should be true, since, if it's empty:
    v.begin() == v.end()
    v.size() == 0
    So
    v.begin() + v.size() = v.begin() + 0 = v.begin() = v.end()

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