List in C++ - Some Useful Functions
Last Updated :
23 Jul, 2025
Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, the List has slow traversal, but once a position has been found, insertion and deletion is quick.
List Useful Functions:
1. emplace(position, value): This function is used to insert an element at the specified position.
2. emplace_back(value) :- This function adds value at end of list. It is different from push_back() by the fact that it directly creates elements at position, whereas push_back() first makes a temporary copy and copies from there. emplace_back() is faster in implementation than push_back() in most situations.
3. emplace_front(value): This function adds value at beginning of the List. It is different from push_front() by the fact that it directly creates elements at position, whereas push_front() first makes a temporary copy and copies from there. emplace_front() is faster in implementation than push_front() in most situations.
CPP
// C++ code to demonstrate the working of
// emplace(), emplace_front() and emplace_back()
#include <iostream>
#include <list> // for list functions
using namespace std;
// Driver Code
int main()
{
// Declaring a list
list<int> gqlist;
// Initialising list iterator
list<int>::iterator it = gqlist.begin();
// Entering list element using emplace_back()
for (int i = 1; i <= 5; i++)
gqlist.emplace_back(i);
// Displaying list elements
cout << "List after emplace_back operation is : ";
for (int& x : gqlist)
cout << x << " ";
cout << endl;
// Entering list element using emplace_front()
for (int i = 10; i <= 50; i += 10)
gqlist.emplace_front(i);
// Displaying list elements
cout << "List after emplace_front operation is : ";
for (int& x : gqlist)
cout << x << " ";
cout << endl;
// using advance() to advance iterator position
advance(it, 2);
// inserting element at 2nd position using emplace()
gqlist.emplace(it, 100);
// Displaying list elements
cout << "List after emplace operation is : ";
for (int& x : gqlist)
cout << x << " ";
cout << endl;
return 0;
}
OutputList after emplace_back operation is : 1 2 3 4 5
List after emplace_front operation is : 50 40 30 20 10 1 2 3 4 5
List after emplace operation is : 50 100 40 30 20 10 1 2 3 4 5
Time Complexity: O(1)
Auxiliary Space: O(1)
4. merge(list2): This function is used to merge list2 with list1. If both the lists are in sorted order, then the resulting list is also sorted.
5. remove_if(condition): This function removes the element from the List on the basis of the condition given in its argument.
CPP
// C++ code to demonstrate the working of
// merge() and remove_if()
#include <iostream>
#include <list> // for list functions
using namespace std;
// Driver Code
int main()
{
// Initializing list1
list<int> gqlist1 = { 1, 2, 3 };
// Initializing list2
list<int> gqlist2 = { 2, 4, 6 };
// using merge() to merge list1 with list2
gqlist1.merge(gqlist2);
// Displaying list elements
cout << "list1 after merge operation is : ";
for (int& x : gqlist1)
cout << x << " ";
cout << endl;
// using remove_if() to remove odd elements
// removes 1 and 3
gqlist1.remove_if([](int x) { return x % 2 != 0; });
// Displaying list elements
cout << "list1 after remove_if operation is : ";
for (int& x : gqlist1)
cout << x << " ";
cout << endl;
return 0;
}
Outputlist1 after merge operation is : 1 2 2 3 4 6
list1 after remove_if operation is : 2 2 4 6
Time Complexity: O(1)
Auxiliary Space: O(1)
6. unique(): This function is used to delete the repeated occurrences of the number. List has to be sorted for this function to get executed.
7. splice(position, list2): This function is used to transfer elements from one list into another.
CPP
// C++ code to demonstrate the working of
// unique() and splice()
#include <iostream>
#include <list> // for list functions
using namespace std;
// Driver Code
int main()
{
// Initializing list1
list<int> gqlist1 = { 1, 1, 1, 2, 2, 3, 3, 4 };
// Initializing list2
list<int> gqlist2 = { 2, 4, 6 };
// Initializing list1 iterator
list<int>::iterator it = gqlist1.begin();
// using advance() to increment iterator position
advance(it, 3);
// Displaying list elements
cout << "list1 before unique operation is : ";
for (int& x : gqlist1)
cout << x << " ";
cout << endl;
// using unique() to remove repeating elements
gqlist1.unique();
// Displaying list elements
cout << "list1 after unique operation is : ";
for (int& x : gqlist1)
cout << x << " ";
cout << endl << endl;
// using splice() to splice list2 in list1 at position
// it inserts list2 after 2nd position
gqlist1.splice(it, gqlist2);
// Displaying list elements
cout << "list1 after splice operation is : ";
for (int& x : gqlist1)
cout << x << " ";
cout << endl;
return 0;
}
Outputlist1 before unique operation is : 1 1 1 2 2 3 3 4
list1 after unique operation is : 1 2 3 4
list1 after splice operation is : 1 2 4 6 2 3 4
Time Complexity: O(1)
Auxiliary Space: O(1)
8. swap(list2): This function is used to swap one list element with other.
CPP
// C++ code to demonstrate the working of
// swap()
#include <iostream>
#include <list> // for list functions
using namespace std;
// Driver Code
int main()
{
// Initializing list1
list<int> gqlist1 = { 1, 2, 3, 4 };
// Initializing list1
list<int> gqlist2 = { 2, 4, 6 };
// Displaying list before swapping
cout << "The contents of 1st list "
"before swapping are : ";
for (int& x : gqlist1)
cout << x << " ";
cout << endl;
cout << "The contents of 2nd list "
"before swapping are : ";
for (int& x : gqlist2)
cout << x << " ";
cout << endl;
// Use of swap() to swap the list
gqlist1.swap(gqlist2);
// Displaying list after swapping
cout << "The contents of 1st list "
"after swapping are : ";
for (int& x : gqlist1)
cout << x << " ";
cout << endl;
cout << "The contents of 2nd list "
"after swapping are : ";
for (int& x : gqlist2)
cout << x << " ";
cout << endl;
return 0;
}
OutputThe contents of 1st list before swapping are : 1 2 3 4
The contents of 2nd list before swapping are : 2 4 6
The contents of 1st list after swapping are : 2 4 6
The contents of 2nd list after swapping are : 1 2 3 4
Time Complexity: O(1)
Auxiliary Space: O(1)
9. reverse(): This function is used to reverse the order of elements in a list.
C++
#include <iostream>
#include <list>
int main()
{
// Create a list of integers with 5 elements
std::list<int> mylist{ 1, 2, 3, 4, 5 };
// Print the original list
std::cout << "Original list: ";
for (auto it = mylist.begin(); it != mylist.end();
it++) {
std::cout << *it << " ";
}
std::cout << std::endl;
// Reverse the order of elements in the list
mylist.reverse();
// Print the reversed list
std::cout << "Reversed list: ";
for (auto it = mylist.begin(); it != mylist.end();
it++) {
std::cout << *it << " ";
}
std::cout << std::endl;
// Return 0 to indicate successful completion of the
// program
return 0;
}
OutputOriginal list: 1 2 3 4 5
Reversed list: 5 4 3 2 1
Time Complexity: O(1) where n is the number of elements in the list. This is because the function simply iterates over the list once and swaps the pointers for each element.
Auxiliary Space: O(1) where it does not use any extra memory beyond a few temporary variables used for swapping pointers.
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
C++ Standard Template Library (STL) The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith
9 min read
C++ Data Types Data types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.C++ supports a wide variety of data typ
7 min read
Map in C++ STL In C++, maps are associative containers that store data in the form of key value pairs sorted on the basis of keys. No two mapped values can have the same keys. By default, it stores data in ascending order of the keys, but this can be changes as per requirement.Example:C++#include <bits/stdc++.h
8 min read
C++ Classes and Objects In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. We will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program.C++ ClassesA class is a user-defined data type, which holds its own data members and
9 min read