WikiGalaxy

Personalize

Introduction to C++ Lists

What are C++ Lists?

C++ Lists are a part of the Standard Template Library (STL) and are used to store a sequence of elements. They are implemented as doubly linked lists, allowing efficient insertion and deletion of elements.

Creating a List in C++

Basic List Creation

To create a list in C++, include the <list> header and use the std::list class. You can specify the type of elements the list will hold.


#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> myList;
    return 0;
}
    

Adding Elements to a List

Using push_back and push_front

Elements can be added to the end of the list using push_back and to the beginning using push_front.


#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> myList;
    myList.push_back(10);
    myList.push_front(20);
    return 0;
}
    

Accessing List Elements

Iterating through a List

Use iterators to access and iterate through list elements. Iterators are similar to pointers and can be used with loops.


#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> myList = {10, 20, 30};
    for(list<int>::iterator it = myList.begin(); it != myList.end(); ++it) {
        cout << *it << " ";
    }
    return 0;
}
    

Removing Elements from a List

Using pop_back, pop_front, and erase

Elements can be removed from the end using pop_back, from the start using pop_front, or from a specific position using erase.


#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> myList = {10, 20, 30};
    myList.pop_back();
    myList.pop_front();
    myList.erase(myList.begin());
    return 0;
}
    

Sorting a List

Using the sort Function

The sort function sorts the elements of the list in ascending order. It uses the default comparison operator.


#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> myList = {30, 10, 20};
    myList.sort();
    return 0;
}
    

Merging Lists

Using the merge Function

Two lists can be merged into a single list using the merge function. The lists should be sorted prior to merging.


#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> list1 = {10, 20, 30};
    list<int> list2 = {15, 25};
    list1.merge(list2);
    return 0;
}
    

Splicing Lists

Using the splice Function

The splice function transfers elements from one list to another. It can be used to move entire lists or specific elements.


#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> list1 = {10, 20, 30};
    list<int> list2 = {40, 50};
    list1.splice(list1.end(), list2);
    return 0;
}
    

Reversing a List

Using the reverse Function

The reverse function reverses the order of elements in the list, allowing for backward traversal.


#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> myList = {10, 20, 30};
    myList.reverse();
    return 0;
}
    

Unique Elements in a List

Using the unique Function

The unique function removes consecutive duplicate elements in a list, ensuring each element appears only once in sequence.


#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> myList = {10, 10, 20, 30, 30};
    myList.unique();
    return 0;
}
    
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025