C++ code to demonstrate forward list part - 3

Suman Barik
0

// C++ code to demonstrate working of

// insert_after(), emplace_after()

// and erase_after()

#include <forward_list>

#include <iostream>

using namespace std;


// Driver Code

int main()

{

// Initializing forward list

forward_list<int> flist = { 10, 20, 30 };


// Declaring a forward list iterator

forward_list<int>::iterator ptr;


// Inserting value using insert_after()

// starts insertion from second position

ptr = flist.insert_after(flist.begin(), { 1, 2, 3 });


// Displaying the forward list

cout << "The forward list after insert_after operation "

": ";

for (int& c : flist)

cout << c << " ";

cout << endl;


// Inserting value using emplace_after()

// inserts 2 after ptr

ptr = flist.emplace_after(ptr, 2);


// Displaying the forward list

cout << "The forward list after emplace_after "

"operation : ";

for (int& c : flist)

cout << c << " ";

cout << endl;


// Deleting value using erase.after Deleted 2

// after ptr

ptr = flist.erase_after(ptr);

cout << "The forward list after erase_after operation "

": ";

// Displaying the forward list

for (int& c : flist)

cout << c << " ";

cout << endl;




// Deleting value in a range

// another varient of erase which uses start and end iterator

// and deletes all the values in between them

ptr=flist.erase_after(flist.begin(), flist.end());

cout << "The forward list after erase_after (range) operation "

": ";

// Displaying the forward list

for (int& c : flist)

cout << c << " ";

cout << endl;


return 0;

}


Tags

Post a Comment

0Comments
Post a Comment (0)