The front() function can produce the address to the first element of the given specified vector and that can execute in the constant time. For an example, the bellowed code that can uses a vector and the sort()_algorithm to view the first word that can be entered by a user in an alphabetical order :
Code:
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 can the time for all good men to come to the aid of their country
…the above code displays:
Code:
In alphabetical order, the first word is 'aid'.
Bookmarks