forward_list::emplace_front() in C++STL

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.

// INTEGER FORWARD LIST EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#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);
  // forward list becomes 6, 5, 4, 3, 2, 1
  
  // printing the forward list
  for (auto it = myflist.begin(); it != myflist.end(); ++it)
    cout << ' ' << *it;
  
  return 0;
}

Output:



6 5 4 3 2 1
// STRING FORWARD LIST EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#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");
  // forward list becomes This, is, Geeksforgeeks
  
  // printing the forward list
  for (auto it = myflist.begin(); it != myflist.end(); ++it)
    cout << ' ' << *it;
  
  return 0;
}

Output:

This is Geeksforgeeks
// CHARACTER FORWARD LIST EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#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');
  // forward list becomes a, b, x, y, z
  
  // printing the forward list
  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
// CPP program to illustrate
// application Of emplace_front() function
#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);
  
  // Forward list becomes
  // 7, 89, 45, 6, 24, 58, 43
  
  // Sorting function
  myforwardlist.sort();
  
  for (auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)
    cout << ' ' << *it;
}

Output

6 7 24 43 45 58 89


My Personal Notes arrow_drop_up


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :



Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.