front

C/C++ Reference

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: