std::vector iterator

suggest change

begin() returns an iterator to the first element, or end() if the vector is empty. Dereferencing end() is undefined.

end() returns an iterator one past the last element.

If the vector object is const, both begin() and end() return a const_iterator. cbegin() and cend() (C++11) return a const_iterator even when the vector is not const.

Example:

#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = { 1, 2, 3, 4, 5 };  //initialize vector using an initializer_list

    for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
        std::cout << *it << " ";
    }

    return 0;
}

Output:

1 2 3 4 5

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents