C++ Using STL Lists (Part 1) Program
In this tutorial, we will learn about the working of Lists (Part 1), in the C++ programming language.
To understand the basic functionality of the Lists in C++, we will recommend you to visit https://www.studytonight.com/cpp/stl/stl-container-list, where we have explained this concept in detail from scratch.
For better understanding of it's implementation, refer to the well commented CPP code given below.
using namespace std;
//Function to print the elements of the list
void show(list<int> &l)
//Defining an iterator for the list
for (i = l.begin(); i != l.end(); i++)
cout << "\t" << *i;
cout << endl;
cout << "\n\nWelcome to Studytonight :-)\n\n\n";
cout << " ===== Program to demonstrate the working of Lists (Part 1), in CPP ===== \n\n";
//List declaration (list of integers)
list<int> l1, l2;
//Filling the elements
cout << "Filling both the lists in two possible ways: from front and from back\n\n";
for (i = 0; i < 5; i++)
l1.push_back(i * 10); //inserting elements from end
l2.push_front(i * 20); //inserting elements from front
cout << "List 1 whose element is inserted from end is: ";
cout << "\n\nList 1 whose element is inserted from front is: ";
cout << "\n\nThe first element of the list 1 is : " << l1.front();
cout << "\n\nThe last element of the list 2 is : " << l2.back();
cout << "\n\nAfter deleting the first element of list 1, it becomes: ";
cout << "\n\nAfter deleting the last element of list 2, it becomes: ";
cout << "\n\n\n";
We hope that this post helped you develop a better understanding of the concept of Lists and its implementation in C++. For any query, feel free to reach out to us via the comments section down below.
Keep Learning : )