# forward_list emplace_after() and emplace_front() in C++ STL

C++Server Side ProgrammingProgramming

Given is the task to show the working of forward_list::emplace_after() and forward_list::emplace_front() functions in c++.

A forward_list only keeps linkage with the next element unlike normal list that keeps linkage with the next as well as the preceding elements, which helps iterations in both directions. But forward_list can only iterate in the forward direction.

The forward_list::emplace_after() and forward_list::emplace_front() functions are a part of the c++ standard library.

The forward_list::emplace_after() function is used to insert a new element inside a list after the element whose position is specified inside the argument

The forward_list::emplace_front() function is used to insert an element in the beginning of the list.

<forward_list> header file should be included to call the function.

## Syntax

Forward_List_Name.emplace_after(iterator , element);

## Parameters

The function accepts two parameters −

Iterator, the iterator contains the position at which the new element has to be placed.

Element, it contains the element that has to be placed.

## Return Value

The function returns an iterator that points to the new element that has been placed inside the forward list.

## Syntax

Forward_List_Name.emplace_front(element);

## Parameters

The function accepts one parameter.

## Return Value

The function does not return anything.

## Example

Input: 11,34,56
Output: 41 11 34 56

Explanation

Here we created a forwards list Lt with elements 11,34,56. Then we called the emplace_front() function that is used to insert a new element at the beginning of a forward list and here that element is 41.

So when we print the forward list, the output generated is 41 11 34 56 which has the first element as 41.

Approach used in the below program as follows

• First create a list, let us say “Lt” of type int and assign it some values.
• Then call the function emplace_front() to place a new element at the beginning of the list.
• Then create an object of type auto, let us say “itr” that will work as our iterator to store the position to be passed into the emplace_after() position, next to which our new element will be placed. Give itr a location, let us say, the end of the list.
• Then call the emplace_after() function to enter the element at a specified position. The first argument should be the iterator “itr” that specifies the position in the list and the second argument should be the element to be placed at that position.

## Algorithm

Start
Step 1->In function main()
Initialize forward_list<int> Lt={}
Call function Lt.emplace_front()
Initialize auto itr=Lt.end();
Call Lt.emplace_after(itr , element)
End
Stop

## Example

#include<iostream>
#include<list>
using namespace std;
int main() {
forward_list<int> Lt = { 5,6,7,8 };
//Using the emplace_front() function to place element at the beginning.
Lt.emplace_front(3);
auto itr = Lt.end();
/*Using the emplace_after() function to place an element after the location specified*/
Lt.emplace_after(itr , 10)
//Displaying the list
for(auto itr = Lt.begin() ; itr!=Lt.end ; itr++)
cout<<” *itr ”<<” ”;
return 0;
}

## Output

If we run the above code it will generate the following output −

3 5 6 7 8 10
Published on 20-Jan-2020 10:13:50
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.16703100502490997, "perplexity": 2685.597843182897}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-49/segments/1637964362992.98/warc/CC-MAIN-20211204124328-20211204154328-00065.warc.gz"}