How to Compare Two Lists in C++ STL? Last Updated : 21 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In C++, lists are containers provided by the STL library of C++, which allows us to store elements of the same data type in non-contiguous memory locations. Comparing two lists is a very common operation while using lists. In this article, we will learn how to compare two lists in C++. Example: Input: list1 = {10, 20, 30, 40, 50} list2 = {10, 20, 30, 40, 50}Output: Both the lists are equal.Comparing Two Lists in C++To compare two std::list in C++, we can simply use the equality operator (==) that compares all the corresponding elements in the two lists from start to end and if all pairs of elements in both lists are equal it returns true otherwise false. Syntax to Compare Two Lists in C++list1 == list2Here, list1 and list2 are the name of the two list containers.Return value: Boolean true or false.C++ Program to Compare Two Lists The below program demonstrates how to compare two std::list containers in C++. C++ // C++ program to compare two lists #include <iostream> #include <list> using namespace std; int main() { // Initializing two lists of integers list<int> list1 = { 10, 20, 30, 40, 50 }; list<int> list2 = { 10, 20, 30, 40, 50 }; // Comparing the two lists bool equal = (list1 == list2); if (equal == true) { cout << "Both the lists are equal." << endl; } else { cout << "Both the lists are not equal." << endl; } return 0; } OutputBoth the lists are equal. Time Complexity: O(N), where N is the size of two lists.Auxiliary Space: O(1), as no extra space is used. Comment More infoAdvertise with us Next Article How to Compare Two Lists in C++ STL? D denzirop9v Follow Improve Article Tags : C++ Programs C++ STL cpp-list cpp-list-functions CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Compare Two Stacks in C++? In C++, a stack is a type of data structure where elements are inserted and removed according to the Last-In-First-Out (LIFO) principle. In this article, we will see how to compare two stacks in C++. Example: Input: stack1 = {10,20,30,40,50}stack2 = {10,20,30,40,50}Output:Stacks are equalComparing T 2 min read How to Copy a List in C++ STL? In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows us to store data in non-contiguous memory locations efficiently. In this article, we will learn how to copy one list to another in C++. Input: sourceList = {10, 20, 30, 40, 50};Output: 2 min read How to Sort a List in C++ STL? In C++, a list is a sequence container provided by the STL library of C++ that provides the features of a doubly linked list and stores the data in non-contiguous memory locations efficiently. In this article, we will learn how to sort a list in C++. Example: Input: myList = {30, 10, 20, 40, 50};Out 2 min read How to Compare Two Deques in C++? In C++ the Standard Template Library (STL) provides a container called deque (short for double-ended queue) that allows fast insertions and deletions at both ends of the deque. In this article, we will learn how to compare two deques in C++. Example: Input: deque1 = {10,20,30}; deque2 = {10,20,30}; 2 min read How to Declare a List in C++? In C++, list is a data structure used to store elements sequentially in non-contiguous memory locations. This container implements doubly linked list which contains pointers to both previous and next elements in the sequence. In this article, we will learn how to declare a list in C++. Declare a Lis 2 min read How to Sort a List of Pairs in C++? In C++, the pair container allows the users to store two different types of objects as a single unit. We can store the pairs in a list if we want to store multiple pairs in a single place. Lists are sequence containers that allow non-contiguous memory allocation. In this article, we will learn how t 4 min read How to Compare Arrays in C++? In C++, arrays are linear data structures that can store data of the same type in contiguous memory locations. In this article, we will learn how to compare two arrays to check whether they are equal or not in C++. Example: Input: arr1[] = {1, 2, 4, 3, 5, 11} arr2[] = {1 2, 3, 4 ,5} Output: arr1 and 2 min read How to Find the Difference of Two Sets in C++? In C++, the set container provides an efficient way to store unique elements in sorted order. Finding the difference between two sets involves determining the elements that are present in one set but not in the other. In this article, we are going to learn how to find the difference of two sets in C 2 min read How to Find Intersection of Two Sets in C++? In C++, sets are containers that store unique elements following a specific order. The intersection of two datasets includes all the elements that are common in both sets. In this article, we will learn how to find the intersection of two sets in C++ STL. Example:Input: mySet1 = {10,20,30,40,50,60} 2 min read Like