begin

Syntax:

    #include <vector>
    iterator begin();
    const_iterator begin() const;

The function begin() returns an iterator to the first element of the vector, and runs in constant time.

For example, the following code uses begin() to initialize an iterator that is used to traverse the elements of a vector:

    vector<string> words;
    string str;
 
    while( cin >> str ) words.push_back(str);
 
    for( vector<string>::const_iterator iter = words.begin();
         iter != words.end(); ++iter ) {
      cout << *iter << endl;
    }

When given this input:

    hey mickey you're so fine

…the above code produces the following output:

    hey
    mickey
    you're
    so
    fine

Related Topics: [] operator, at, end, rbegin, rend