Implement the set_intersection() in C++
I am working in the MNC company as software programmer. We can use the C++ programming language for developing the software at my work place. Thus, I could not know much more about the set_intersection() function of the C++ language. So, I would like to know about the set_intersection() function of the C++ programming language. I also would like to know about how to implement the set_intersection() function of the C++ programming language. Can anyone has the answer for me then reply me. I am waiting for your reply!!!
The set_intersection() in C++
The set_intersection() function algorithm can computes the intersection of the given two sets that can be defined by range [strt1,nd1) and the range [strt2,nd2) and The set_intersection() function can stores the intersection beginning at result. Both the sets, can be given as ranges that can must be sorted in an ascending order. The set_intersection() function can returns value of an iterator that can be to the end of the intersection range. The set_intersection() function can runs in a linear time.
Implement the set_intersection() in C++
The following program can demonstrates you how to implement the set_intersection() function of the C++ language in the C++ programming as follows :
Code:
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main ()
{
int fs[] = {5,1,35,2,45};
int sd[] = {5,4,3,2,1};
vector<int> r(10);
vector<int>::iterator c;
sort (fs,fs+5);
sort (sd,sd+5);
c=set_intersection (fs, fs+5, sd, sd+5, r.begin());
cout << "intersection has " << int(c - r.begin()) << " elements.\n";
return 0;
}
The above code can generate the following Output :
Code:
intersection has 2 elements
set_intersection() in C++
The set_intersection() function can be used to constructs a range that can be sorted starting in the place that can be pointed by result with the given set of intersection of the two ranges [fst1,lst1) and range [fst2,lst2) that can be sorted as content. The intersection of two given sets can be formed only by the elements that can represent in both of the sets at the same time. The following can be the syntax of the set_intersection() function in the C++ language as follows :
Code:
template <class InptItrtr1, class InptItrtr2, class OtptItrtr>
OtptItrtr set_intersection ( InptItrtr1 first1, InptItrtr1 last1,
InptItrtr2 first2, InptItrtr2 last2,
OtptItrtr result );
Re: Implement the set_intersection() in C++
The following can be the parameters of the set_intersection() function of the C++ language as :
1. fst2, lst2 : These parameters of the set_intersection() function can contains an input iterators to the final and starting positions of the given second sequence. The range used can be as [fst2,lst2).
2. fst1, lst1 : These parameters of the set_intersection() function can contains an input iterators to the final and starting positions of the given first sequence. The range used can be as [fst1,lst1) that can contains all of an elements between fst1 and lst1 including an element that can be pointed by fst1 but not an element that can be pointed by lst1.
Re: Implement the set_intersection() in C++
The code that can be mentioned below also states you about the working of the set_intersection() function of the C++ language :
Code:
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
bool result;
string h("abcde");
string h2("aeiou");
vector<char> vt1(h.begin(), h.end());
vector<char> vt2(h2.begin(), h2.end());
vector<char> setIntersection;
set_intersection(vt1.begin(), vt1.end(),vt2.begin(), vt2.end(),
back_inserter(setIntersection));
for(int g=0; g<setIntersection.size(); g++)
{
cout << setIntersection[g];
}
return 0;
}