![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ List::emplace_front() Function
The C++ std::list::emplace_front() function is used to insert new element at the beginning of list.
It inserts the specified value at the beginning of the current list and increases the list size by one. The return type of the emplace_front() function is void which implies that it does not return any value.
The emplace_front() function is similar to the push_front() function, where the push_front() function inserts the given element and adds the copy of the object at the beginning of the list, whereas the emplace_front() function inserts element and constructs the object in place at the beginning of the list, potentially improving performance by avoiding a copy operation.
Syntax
Following is the syntax of the std::list::emplace_front() function −
void emplace_front (val);
Parameters
- val − It is a new value of element to be inserted in the list.
Return Value
This function does not return any value.
Example 1
In the following program, we are using the C++ std::list::emplace_front() function to insert a new element value 1 at the beginning of the current list {2, 3, 4, 5}.
#include<iostream> #include<list> using namespace std; int main() { //create a list list<int> num_list = {2, 3, 4, 5}; cout<<"List elements before emplace_front operation: "; for(int n : num_list) { cout<<n<<" "; } //element value int val = 1; //using emplace_front() function num_list.emplace_front(val); cout<<"\nList elements after emplace_front() operation: "<<endl; for(int n : num_list) { cout<<n<<" "; } return 0; }
Output
The above program produces the following output −
List elements before emplace_front operation: 2 3 4 5 List elements after emplace_front() operation: 1 2 3 4 5
Example 2
Apart from the int element, you can also insert the char element in the list(type char).
Following is another example of the C++ std::list::emplace_front() function. Here, we are creating a list(type char) named vowels with the value {'e', 'i', 'o', 'u'}. Then, using the emplace_front() function, we are trying to insert a new char element 'a' at the beginning of this list.
#include<iostream> #include<list> using namespace std; int main() { //create a list list<char> vowels = {'e', 'i', 'o', 'u'}; cout<<"List elements before emplace_front operation: "; for(char v : vowels) { cout<<v<<" "; } //element value char val = 'a'; //using emplace_front() function vowels.emplace_front(val); cout<<"\nList elements after emplace_front() operation: "<<endl; for(char v : vowels) { cout<<v<<" "; } return 0; }
Output
On executing the above program, it will generate the following output −
List elements before emplace_front operation: e i o u List elements after emplace_front() operation: a e i o u
Example 3
Insert a string element in the list(type string).
In this example, we are creating a list(type string) named fruits with the values {"Orange", "Banana", "Grapes", "Apple"}. Then, using the emplace_front() function, we are trying to insert the string element "Papaya" at the beginning of this list.
#include<iostream> #include<list> using namespace std; int main() { //create a list list<string> fruits = {"Orange", "Banana", "Grapes", "Apple"}; cout<<"List elements before emplace_front operation: "; for(string f : fruits) { cout<<f<<" "; } //element value string val = "Papaya"; //using emplace_front() function fruits.emplace_front(val); cout<<"\nList elements after emplace_front() operation: "<<endl; for(string f : fruits) { cout<<f<<" "; } return 0; }
Output
This will generate the following output −
List elements before emplace_front operation: Orange Banana Grapes Apple List elements after emplace_front() operation: Papaya Orange Banana Grapes Apple
Example 4
In the following example, we are creating a list(type int) named numbers with the values {5,6,7,8,9,10}. Then, we are using the emplace_front() function inside the for loop to insert numbers dynamically at the beginning in the current list.
#include<iostream> #include<list> using namespace std; int main() { //create a list list<int> numbers = {5,6,7,8,9,10}; cout<<"List elements before emplace_front operation: "; for(int n : numbers ) { cout<<n<<" "; } //using emplace_front() function for(int i = 5; i>=1; i--) { numbers.emplace_front(i); } cout<<"\nList elements after emplace_front operation: "<<endl; for(int n : numbers ) { cout<<n<<" "; } return 0; }
Output
Following is the output of the above program −
List elements before emplace_front operation: 5 6 7 8 9 10 List elements after emplace_front operation: 1 2 3 4 5 5 6 7 8 9 10