![]() |
In C++, vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted. In this article, we will learn how to get the difference between two vectors in C++. Example Input: vec1 = { 1, 2, 3, 4, 5 }; vec2 = { 3, 4, 5, 6, 7 }; Output: Difference: {1, 2} Finding the Difference Between Two VectorsThe difference between two vectors refers to the elements of the first vector that are not in the second vector. In C++, the std::set_difference function from the <algorithm> header can be used to find the difference between two vectors. The set_difference algorithm takes two sorted ranges and outputs a new range containing the elements present in the first range but not in the second range. Syntax of std::set_differenceset_difference(first1, last1, first2, last2, d_first);
C++ Program to Find the Difference of Two VectorsC++
Output
Difference: 1 2 Time Complexity: O(NlogN + MlogM), where N and M are the sizes of the input vectors. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |