C++ list push_front() Function
Example
Add an element at the beginning of a list:
list<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars.push_front("Tesla");
for (string car : cars) {
cout << car << "\n";
}
Definition and Usage
The push_front() function adds an element to the beginning of a list. The new element becomes the first element.
The push_front function is defined in the <list> header file.
Syntax
list.push_front(<type> value);
<type> refers to the type of the data that the list contains.
Parameter Values
| Parameter | Description |
|---|---|
| value | Required. The value of the element to add. |
Technical Details
| Returns: | Nothing (void). |
|---|
Related Pages
Read more about lists in our List Tutorial.