List in STL C++

About List in STL :

List in STL is defined as the container used to store elements in the contiguous manner and allow insertion and deletion at any position.

List in STL

About List STL :

In STL, List store the elements on a contiguous memory, but vector stores on a non-contiguous memory. List is a contiguous container, whereas vector is a non-contiguous container. As it takes a long time to move all the elements, insertion and deletion in the midst of the vector are quite expensive. This issue is solved by linklist, which is implemented using a list container. The list offers an effective method for insertion and deletion operations and facilitates bidirectional communication. List traversal is longer because elements are read sequentially, whereas a vector allows for random access.

Functions in List :

 FunctionWorking
push_back()insert an element to the back end of the list.
push_front()insert an element to the front end of the list.
size()give the number of elements present in the list.
empty()tells whether thelist is empty or not.
pop_front()

remove the element from front of the list.

Basic Operations with  List

Creating a List

To use the C++ list STL, include the &ltlist&gt header file and declare a list object. For example:

#include <list>
std::list<int> myList;

Adding Elements

Elements can be added to the list using the push_back() and push_front() methods for adding to the end and beginning, respectively.

myList.push_back(42);
myList.push_front(10);

Removing Elements

Use the pop_back() and pop_front() methods to remove elements from the list.

myList.pop_back();
myList.pop_front();

Accessing Elements

Access elements using iterators. Here’s an example of iterating through the list:

for (std::list < int >::iterator it = myList.begin(); it != myList.end(); ++it) {
    std::cout << *it << " ";
}

Advanced Operations with List

Sorting

The list can be sorted using the sort() method from the algorithm header.

 myList.sort();
						

Merging Lists

Merging combines two sorted lists into a single sorted list.

std::list < int > newList;
myList.merge(newList);

Reversing

Reverse the elements in the list using the reverse() method.

myList.reverse();

Practical Examples of C++ List STL Usage

Example 1: Implementing a To-Do List

Run
#include < iostream >
#include < list >

int main() {
    std::list todoList;
    todoList.push_back("Finish report");
    todoList.push_back("Buy groceries");
    todoList.push_back("Go to the gym");

    for (const auto& task : todoList) {
        std::cout << task << std::endl;
    }

    return 0;
}

Example 2: Merging Sorted Lists

Run
#include < iostream >
#include < list >

int main() {
    std::list list1 = {1, 3, 5};
    std::list list2 = {2, 4, 6};

    list1.sort();
    list2.sort();

    list1.merge(list2);

    for (const auto& num : list1) {
        std::cout << num << " ";
    }

    return 0;
}

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription