Share via


vector::cbegin

Returns a const random-access iterator to the first element in the container.

const_iterator cbegin() const;

Return Value

A const random-access iterator addressing the first element in the vector Class or to the location succeeding an empty vector. You should always compare the value returned with vector::cend or vector::end to ensure it is valid.

Remarks

With the return value of cbegin, the vector object cannot be modified.

Example

// vector_cbegin.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main()
{
    using namespace std;
    vector<int> c1;
    vector<int>::const_iterator c1_Iter;

    c1.push_back(1);
    c1.push_back(2);

    cout << "The vector c1 contains elements:";
    c1_Iter = c1.cbegin();
    for (; c1_Iter != c1.cend(); c1_Iter++)
    {
        cout << " " << *c1_Iter;
    }
    cout << endl;

    // The following line would be an error because iterator is const
    // *c1.cbegin() = 200;
}
The vector c1 contains elements: 1 2

Requirements

Header: <vector>

Namespace: std

See Also

Reference

vector Class

Standard Template Library

Other Resources

vector Members