front [C++ Reference]
Keywords: stl,vector,front
Date: 2008-10-14T15:42:10-0700

[[front]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- fr
- it
- jp
- pl
- ru
- tr

## front

Syntax:

        #include <vector>
        TYPE& front();
        const TYPE& front() const;

The front() function returns a reference to the first element of the vector, and runs in constant time.

For example, the following code uses a vector and the sort()_algorithm to display the first word (in alphabetical order) entered by a user:

        vector<string> words;
        string str;

        while( cin >> str ) words.push_back(str);

        sort( words.begin(), words.end() );

        cout << "In alphabetical order, the first word is '" << words.front() << "'." << endl;

When provided with this input:

        now is the time for all good men to come to the aid of their country

…the above code displays:

        In alphabetical order, the first word is 'aid'.

Related Topics: back

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •