This documentation is archived and is not being maintained.

vector::cbegin

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

const_iterator cbegin() const;

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.

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

// 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

Header: <vector>

Namespace: std

Show: