C++ Forward_list::emplace_after() Function



The C++ std::forward_list::emplace_after() function is used to insert(or append) a new element just after the specified position in the forward_list container.

It returns an iterator that points to the newly inserted element and increases the forward_list size by one. In C++, an iterator is an object that can iterate over elements in an STL(standard template library) container and provided access to individual elements.

We can use the emplace_after() function along with the for-loop to insert an element dynamically after the specified position in the forward_list container.

Syntax

Following is the syntax of the C++ std::forward_list::emplace_after() function −

iterator emplace_after (val, pos);

Parameters

  • val − It is the value of the new element to be inserted into the forward_list.
  • position − It is the position in the forward_list after which new element is to be inserted.

Return value

This function returns an iterator that points to the newly inserted element.

Example 1

Insert an element into the forward_list just after the beginning position.

In the following program, we are using the C++ std::forward_list::emplace_after() function to insert a new specified element 20 just after the beginning position in the current forward_list {10, 30, 40, 50}.

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<int> num_list = {10, 30, 40, 50};
   cout<<"The num_list contents before the emplace_after operation: "<<endl;
   for(int n : num_list){
      cout<<n<<endl;
   }
   //value of element and pos
   int val = 20;
   auto pos = num_list.begin();
   //using the emplace_after() function 
   num_list.emplace_after(pos, val);
   cout<<"The num_list contents after the emplace_after operation: "<<endl;
   for(int n : num_list){
      cout<<n<<endl;
   }
}

Output

Following is the output of the above program −

The num_list contents before the emplace_after operation: 
10
30
40
50
The num_list contents after the emplace_after operation: 
10
20
30
40
50

Example 2

If the forward_list is a char-type, this function inserts the new specified element just after the specified position.

Following is another example of the C++ std::forward_list::emplace_after() function. Here, we are creating a forward_list(type char) named char_list with contents {'B', 'C', 'D', 'E', 'F'}. Then, using the emplace_after() function, we are trying to insert a new specified element 'A' just after the specified position in this forward_list.

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<char> char_list = {'B', 'C', 'D', 'E', 'F'};
   cout<<"The char_list contents before the emplace_after operation: "<<endl;
   for(char c : char_list){
      cout<<c<<endl;
   }
   //value of element and pos
   char val = 'A';
   auto pos = char_list.before_begin();
   //using the emplace_after() function 
   char_list.emplace_after(pos, val);
   cout<<"The char_list contents after the emplace_after operation: "<<endl;
   for(char c : char_list){
      cout<<c<<endl;
   }
}

Output

This will generate the following output −

The char_list contents before the emplace_after operation: 
B
C
D
E
F
The char_list contents after the emplace_after operation: 
A
B
C
D
E
F

Example 3

Apart from the int-type and char-type elements, we can also insert the string-type element just after the specified position.

In this example, we are creating a forward_list(type string) named names with contents {"Aman", "Abhishek", "Ganesh", "Mohit"}. Then, using the emplace_after() function, we are trying to insert a new specified element "Raju" just after the specified position beginning in the current forward_list.

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<string> names = {"Aman", "Abhishek", "Ganesh", "Mohit"};
   cout<<"The names forward_list contents before the emplace_after operation: "<<endl;
   for(string s: names){
      cout<<s<<endl;
   }
   //value of element and pos
   string val = "Raju";
   auto pos = names.begin();
   //using the emplace_after() function 
   names.emplace_after(pos, val);
   cout<<"The names forward_list contents after the emplace_after operation: "<<endl;
   for(string s : names){
      cout<<s<<endl;
   }
}

Output

The above program produces the following output −

The names forward_list contents before the emplace_after operation: 
Aman
Abhishek
Ganesh
Mohit
The names forward_list contents after the emplace_after operation: 
Aman
Raju
Abhishek
Ganesh
Mohit

Example 4

Using the emplace_after() function along with the for-loop to insert an element dynamically into a position after the specified position in the current forward_list.

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<int> num_list = {1};
   cout<<"The size of the num_list before the emplace_after operation: "<<endl;
   int size = distance(num_list.begin(), num_list.end());
   cout<<size<<endl;
   //pos
   auto pos = num_list.begin();
   for(int i = 10; i>1; i--){
      num_list.emplace_after(pos, i);
   }
   cout<<"The num_list contents after the emplace_after operation: "<<endl;
   for(int n : num_list){
      cout<<n<<endl;
   }
}

Output

Let us compile and run the above program, this will produce the following result −

The size of the num_list before the emplace_after operation: 
1
The num_list contents after the emplace_after operation: 
1
2
3
4
5
6
7
8
9
10
Advertisements