Skip to main content

C++ STL List Container – std::list

In this tutorial you will learn about C++ STL list container i.e. std::list and methods which can be applicable on it.

List comes under sequence containers. List stores elements in non-contiguous memory locations. List works same as double linked list. It can traverse in both directions. This is the reason list is slow in traversing when compared to vector. But it supports constant time insertion and removal of elements from anywhere in the container. If we want to implement single linked list then we should use forward list.

The main disadvantage by this list is, unlike other sequence containers elements of this container can’t be accessed by its index position.

C++ STL List

Declaring List

list<data_type> listName;

Operations Applicable on List Container

pus_front(x): It adds the new element ‘x’ at front of the list.

push_back(x): It adds the new element ‘x’ at the end of the list.

insert(): This function inserts the new elements to the list before the element at a specific position.

assign(): This erases the current elements of the list and adds new elements. Due to this replacement list size will change.

begin(): It returns the iterator pointing to the beginning of the list.

end(): This returns the iterator pointing to the last element of the list.

Example program to show ways to insert elements into list:

#include<iostream>
#include<list>
#include<iterator>

using namespace std;

int main(){
        list<int> lst1;
        list <int> :: iterator it;
        
        // inserting elements using push_front
        for(int i=0; i<3; i++)
            lst1.push_front(i);
        
        // inserting elements using push_back
        for(int i=1; i<=3; i++)
            lst1.push_back(i+10);
        
        // adding elements using insert() function
        it = lst1.begin();
        it++;
        lst1.insert(it, 34); // this inserts element 34 in front of iterator points
        
        // ohter way of adding elements using insert() method
        lst1.insert(it, 2, 44); // this inserts two elements of value 44 at where iterator points

        // displaying list
        for(it = lst1.begin(); it != lst1.end(); ++it)
            cout << *it << " ";
    
    cout << endl;
        
        // this is adding elements using assign method
        lst1.assign(5,50);
        // this adds 5 elements of each value 50 by erasing all previous elements of the list. 
        
        // check again
        for(it = lst1.begin(); it != lst1.end(); ++it)
            cout << *it << " ";
    
    cout << endl;
        
        return 0;
}

Output

2 34 44 44 1 0 11 12 13
50 50 50 50 50

Some more functions…

front(): It returns reference to the first element of the list.

back(): It returns reference to the current last element of the list.

pop_front(): This erases the first element of the list.

pop_back(): This erases the last element of the list.

erase(): It removes a single element or range of elements in from the list.

remove(x): It removes the all elements of the list which has value x .

empty(): This is Boolean type method. This returns whether the list is empty or not.

Example program to show all above functions:

#include<iostream>
#include<list>
#include<iterator>

using namespace std;

int main(){
        list <int> lst1;
        list <int> :: iterator it;
        list <int> :: iterator it1;
        
        // inserting some elements into list.
        for(int i=0; i<5; i++)
            lst1.push_front(i+10);
        
        for(it =lst1.begin(); it!= lst1.end(); it++)
            cout << *it << " ";
        
        cout << endl;
        
        // getting front element
        cout << "the first element of the list is ";
        cout << lst1.front() << endl;
        
        // getting last element
        cout << "the last element of the list is ";
        cout << lst1.back() << endl;
        
        // erasing first element of the list
        lst1.pop_front();
        cout << "the first element after erasing current first elemnt is ";
        cout << lst1.front();
        cout << endl;
        
        // erasing last element of the list
        lst1.pop_back();
        cout << "the last element after erasing current last element is ";
        cout << lst1.back();
        cout << endl;
        
        // deleting elements using erase() function
        it = lst1.begin();
        lst1.erase(it); // this removes the element where itertaor points
        
        // displaying remaining elements in the list
        cout << "remaining elements after doing all above operations " << endl;
        for(it =lst1.begin(); it!= lst1.end(); it++)
            cout << *it << " ";
        
        cout << endl;
        
        // checking list is empty or not
        lst1.empty() ? cout << "List is empty" << endl : cout << "List is not empty" << endl;
        
        // applying remove() method
        lst1.remove(11);
        
        // displaying remaining elements in the list
        cout << "remaining elements after removing 11 are" << endl;
        for(it =lst1.begin(); it!= lst1.end(); it++)
            cout << *it << " ";
        
        cout << endl;
        
        return 0;
}

Output

14 13 12 11 10
the first element of the list is 14
the last element of the list is 10
the first element after erasing current first elemnt is 13
the last element after erasing current last element is 11
remaining elements after doing all above operations
12 11
List is not empty
remaining elements after removing 11 are
12

reverse(): This reverse all the elements of the list.

sort(): Sorts the all elements in the list in increasing order.

size(): This returns the number of elements in the list.

Example program to show above functions:

#include<iostream>
#include<list>
#include<iterator>

using namespace std;

int main(){
        list <int> lst1;
        list <int> :: iterator it;
        
        for(int i=0; i<6; i++){
                if(i%2) lst1.push_back(i);
                else lst1.push_front(i);
        }
        
        cout << "actual elements of the list are" << endl;
        for(it = lst1.begin(); it!= lst1.end(); it++)
            cout << *it << " ";
        
        cout << endl;
        
        // using reverse function
        lst1.reverse();
        cout << "Elements in the list after applying reverse operation " << endl;
        for(it = lst1.begin(); it!= lst1.end(); it++)
            cout << *it << " ";
        
        cout << endl;
        
        // using sort function
        lst1.sort();
        cout << "Elements in the list after applying sort operation" << endl;
        for(it = lst1.begin(); it!= lst1.end(); it++)
            cout << *it << " ";

        cout << endl;
        
        // finding size
        cout << "size of the lis is ";
        cout << lst1.size();

        return 0;
}

Output

actual elements of the list are
4 2 0 1 3 5
Elements in the list after applying reverse operation
5 3 1 0 2 4
Elements in the list after applying sort operation
0 1 2 3 4 5
size of the lis is 6

Comment below if you have queries or found information incorrect in above tutorial for C++ STL List.

The post C++ STL List Container – std::list appeared first on The Crazy Programmer.



from The Crazy Programmer http://www.thecrazyprogrammer.com/2017/07/stl-list.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and

Top Tips For PCB Design Layout

Are you thinking about designing a printed circuit board? PCBs are quite complicated, and you need to make sure that the layout that you choose is going to operate as well as you want it to. For this reason, we have put together some top tips for PCB design layout. Keep reading if you would like to find out more about this. Leave Enough Space One of the most important design tips for PCB layout is that you need to make sure that you are leaving enough space between the components. While many people might think that packing components closely is the best route to take, this can cause problems further down the line. This is why we suggest leaving extra space for the wires that will spread. This way, you’ll have the perfect PCB design layout. Print Out Your Layout Struggling to find out if your components sizes match? Our next tip is to print out your layout and compare the printed version to your actual components. Datasheets can sometimes come with errors, so it doesn’t hurt to do