q = {'J', 'I', 'H', 'G', 'F'}
In C++, the intersection of these two sets, would be:
arranged in ascending order based on default settings. Intersection of other set types are possible such as intersection of sets of integers, intersection of sets of floats, intersection of sets of doubles, etc.
The set class in the C++ set library, which should be included into the program for set work, does not have a member function for intersection. So, in order to obtain intersection of sets, the algorithm library, which has the set_intersection() function, has to be included into the program.
The C++ algorithm library has a number of set_intersection overloaded functions. Only the simplest two are explained in this article. However, before the explanations start, the reader has to know the difference between output iterator, input iterator and forward iterator.
OutputIterator and ForwardIterator
An iterator is a class pointer. An OutputIterator is an iterator to which a value can be assigned to with the dereferenced expression. For example, if the iterator is i for integers, then;
would make i point to the memory location that has the value, 5.
An InputIterator is an iterator whose dereferenced expression would return the value the iterator is pointing to. For example, if the iterator is i for integers, and is pointing to the memory location that has the number 7, then;
would make num hold the value, 5.
A ForwardIterator is an elaborated form of the input iterator.
Ranges
When the values intended for a set have been inserted into the set, the values become sorted in ascending order based on default settings. With sets, two forward iterators can be used to identify a range of elements in the set. This article is concerned with the whole range of the set. The following program shows how to obtain the forward iterators that represent the whole range of one set:
#include <set>
using namespace std;
int main()
{
set<char> p = {'H', 'G', 'F', 'E', 'D'};
set<char>::iterator first = p.begin();
set<char>::iterator last = p.end();
return 0;
}
Note the use of the begin() and end() member functions of the set class.
For the purpose of intersection of two complete sets, there will be first1 and last1 for the first set; and first2 and last2 for the second set; for both complete ranges.
Output Iterator
The two set_intersection functions considered in this article return an output iterator. Unfortunately, the set class does not have an output iterator. Well, the vector class has. This means the output iterator of the vector class which is simply called iterator, can be used to receive the output iterator returned by the set_intersection() function. Another good news is that, this vector iterator can serve as both output iterator and input iterator. Do not forget to include the vector in order to use it in the program.
The two set_intersection overloaded functions mentioned above can now be discussed.
Basic Set_intersection Function
The syntax for this function in the algorithm library, is:
constexpr OutputIterator
set_intersection(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, OutputIterator result)
OutputIterator is the return output iterator, obtained from the vector class. It would be pointing just after the last practical element in the vector. This means the size of the empty vector to receive the intersection of sets has to be estimated to be above that of the number of values in the intersection. The last argument result is the output iterator pointer pointing to the start of the vector, which will receive the intersection of sets.
With the vector, the output iterator returned, which also happens to be an input iterator, can be used to display the values of the intersection of sets using the for-loop. With the preceding introduction for this article, the rest of the parameters of the function become self-explanatory. The following program shows how to use this function:
#include <set>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
set<char> p = {'H', 'G', 'F', 'E', 'D'};
set<char>::iterator first1 = p.begin(); set::iterator last1 = p.end();
set<char> q = {'J', 'I', 'H', 'G', 'F'};
set<char>::iterator first2 = q.begin(); set::iterator last2 = q.end();
vector<char> vtr(10);
vector<char>::iterator outIt = set_intersection (first1, last1, first2, last2, vtr.begin());
vtr.resize(outIt - vtr.begin());
for (outIt = vtr.begin(); outIt != vtr.end(); outIt++)
cout << *outIt << ", ";
cout << endl;
return 0;
}
Notice that the vector had to be resized to contain only the elements of the intersection after the set_intersection() function had been called. The output is:
Basic Set_intersection Function with Custom Comparison
The syntax for this function in the algorithm library is:
constexpr OutputIterator
set_intersection(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
OutputIterator is the return output iterator obtained from the vector class. It would be pointing just after the last practical element of the vector. This means the size of the empty vector to receive the intersection of sets has to be estimated to be above that of the number of values in the intersection. The last-but-one argument result is the output iterator pointer pointing to the start of the vector, which will receive the intersection of sets.
With the vector, the output iterator returned, which also happen to be an input iterator, can be used to display the values of the intersection of sets using the for-loop.
Comp, is a programmer defined function. It can be:
if (a != b)
return true;
else
return false;
}
This comp() function returns true or false. From the introduction of this article above, the rest of the parameters of the set_intersection function, are self-explanatory.
With the above program header, the following main() function will use the above comp() function successfully.
{
set<char> p = {'H', 'G', 'F', 'E', 'D'};
set<char>::iterator first1 = p.begin(); set<char>::iterator last1 = p.end();
set<char> q = {'J', 'I', 'H', 'G', 'F'};
set<char>::iterator first2 = q.begin(); set<char>::iterator last2 = q.end();
vector<char> vtr(10);
vector<char>::iterator outIt = set_intersection (first1, last1, first2, last2, vtr.begin(), comp);
vtr.resize(outIt - vtr.begin());
for (outIt = vtr.begin(); outIt != vtr.end(); outIt++)
cout << *outIt << ", ";
cout << endl;
return 0;
}
The output is:
same as before.
Conclusion
The set class in the C++ set library, which should be included into the program for set work, does not have a member function for intersection. So, in order to obtain intersection of sets, the algorithm library, which has the set_intersection() function, has to be included into the program.
The C++ algorithm library has a number of set_intersection overloaded functions. As of January, 2022, two of these functions that have most likely been implemented by your compiler, have been explained above. Compilers are still to implement the rest of the overloaded set_intersection() functions found in the C++ specification.