Thread: std::vector iterators

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

    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,336
    .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