C++ vector front() function
Example
Get the first element of a vector:
vector<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars.front();
Try it Yourself »
Definition and Usage
The front()
function returns a reference to the first element in a vector.
Tip: Values can be assigned to the reference, which will change the vector.
Syntax
vector.front();
Parameter Values
None.
Technical Details
Returns: | A reference to the first element in the vector. |
---|
More Examples
Example
Change the first element in a vector:
vector<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars.front() = "Toyota";
for(string car : cars) {
cout << car << "\n";
}
Try it Yourself »
Related Pages
Read more about vectors in our Vector Tutorial.