How to Find Difference of Two Sorted Vectors in C++? Last Updated : 26 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, vectors are containers similar to arrays but unlike arrays, vectors can change their size dynamically during the insertion or deletion of elements. In this article, we will learn how we can find the difference between two sorted vectors in C++. The difference of two vectors is a vector that contains elements present in the first vector but not in the second. Example: Input:myVector1 = {1, 2, 3, 4, 5}myVector2 = { 3, 4, 5, 7, 8}Output:Difference of two vectors is {1, 2}Find the Difference of Two Sorted Vectors in C++We can easily find the difference between the two vectors using the std:: set_difference() method provided by the STL in C++. But the vectors should be sorted in increasing order so that the std::set_difference() can find the difference between them or else the behavior will be undefined. C++ Program to Find the Difference of Two Sorted Vectors C++ // C++ program Find the Difference of Two Vectors #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { // Initialize two input vectors vector<int> vec1 = { 10, 20, 3, 4, 5 }; vector<int> vec2 = { 3, 4, 5, 7, 8 }; // Print elements of both the vectors cout << "Element in Vector 1: "; for (auto num : vec1) { cout << num << " "; } cout << endl; cout << "Element in Vector 2: "; for (auto num : vec2) { cout << num << " "; } cout << endl; // Define a vector to store the difference vector<int> difference; // Find the difference of vec1 and vec2 set_difference(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), back_inserter(difference)); // Print the difference between the vectors cout << "Difference of two vectors is: "; for (auto element : difference) { cout << element << " "; } return 0; } OutputElement in Vector 1: 10 20 3 4 5 Element in Vector 2: 3 4 5 7 8 Difference of two vectors is: 10 20 3 4 5 Time Complexity: O(N) where N is the number of elements in both the vectorsAuxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Find Difference of Two Sorted Vectors in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 Union of Two Sorted Vectors in C++? The union of two vectors refers to the collection of all the distinct elements from the two vectors. In this article, we will learn how we can find the union of two sorted vectors in C++.The most efficient method to find the union of two sorted vector is by using set_union() method. Letâs take a loo 3 min read How to Find the Difference of Two Deques in C++? In C++, deques containers are sequential containers that allow the insertion and deletion of elements at both the beginning and end. In this article, we will learn how to find the difference between two deques in C++. Example: Input: deque1 = {1, 2, 3, 4, 5}; deque2 = {3, 4, 5, 6, 7}; Output: deque1 2 min read How to Find Symmetric Difference of Two Maps in C++? In C++, a map is a container that stores elements in a mapped fashion. Each element has a key value and a mapped value. The symmetric difference between two sets is formed by the elements that are present in one of the sets, but not in the other. In this article, we will learn how to find the symmet 2 min read How to Find the Median of Sorted Vector in C++? In C++, vectors are dynamic arrays and the median is a middle value when data is sorted in ascending order. In this article, we will learn how to find the median of all elements in a sorted vector in C++. Example Input: myVector = {10, 20, 30, 40, 50}; Output: Median is 30Finding Median of Sorted Ve 2 min read Like