forward_list::emplace_front() in C++STL
Last Updated :
27 Nov, 2018
Forward list in STL implements singly linked list. Introduced from C++11, the forward list is useful than other containers for insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from the list by the fact that forward list keeps track of the location of only next element while list keeps track to both next and previous elements.
forward_list::emplace_front()
This function is used to insert a new element into the forward list container, the new element is added to the beginning of the forward list.
Syntax :
forwardlistname.emplace_front(value)
Parameters :
The element to be inserted into the forward list
is passed as the parameter.
Result :
The parameter is added to the
forward list at the beginning.
Examples:
Input : myflist{1, 2, 3, 4, 5};
myflist.emplace_front(6);
Output : myflist = 6, 1, 2, 3, 4, 5
Input : myflist{};
myflist.emplace_front(4);
Output : myflist = 4
Errors and Exceptions
1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown.
2. The parameter should be of the same type as that of the container otherwise, an error is thrown.
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
forward_list< int > myflist;
myflist.emplace_front(1);
myflist.emplace_front(2);
myflist.emplace_front(3);
myflist.emplace_front(4);
myflist.emplace_front(5);
myflist.emplace_front(6);
for ( auto it = myflist.begin(); it != myflist.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
6 5 4 3 2 1
#include <forward_list>
#include <iostream>
#include <string>
using namespace std;
int main() {
forward_list<string> myflist;
myflist.emplace_front( "Geeksforgeeks" );
myflist.emplace_front( "is" );
myflist.emplace_front( "This" );
for ( auto it = myflist.begin(); it != myflist.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
This is Geeksforgeeks
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
forward_list< char > myflist;
myflist.emplace_front( 'z' );
myflist.emplace_front( 'y' );
myflist.emplace_front( 'x' );
myflist.emplace_front( 'b' );
myflist.emplace_front( 'a' );
for ( auto it = myflist.begin(); it != myflist.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
a b x y z
Time Complexity : O(1)
Application : Input an empty forward list with the following numbers and order using emplace_front() function and sort the given forward list.
Input : 7, 89, 45, 6, 24, 58, 43
Output : 6, 7, 24, 43, 45, 58, 89
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
forward_list< int > myforwardlist{};
myforwardlist.emplace_front(43);
myforwardlist.emplace_front(58);
myforwardlist.emplace_front(24);
myforwardlist.emplace_front(6);
myforwardlist.emplace_front(45);
myforwardlist.emplace_front(89);
myforwardlist.emplace_front(7);
myforwardlist.sort();
for ( auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)
cout << ' ' << *it;
}
|
Output
6 7 24 43 45 58 89
Share your thoughts in the comments
Please Login to comment...