I have been trying to find the intersection between two std::set in C++, but I keep getting an error.
I created a small sample test for this
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int main() {
set<int> s1;
set<int> s2;
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
s2.insert(1);
s2.insert(6);
s2.insert(3);
s2.insert(0);
set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end());
return 0;
}
The latter program does not generate any output, but I expect to have a new set (let's call it s3
) with the following values:
s3 = [ 1 , 3 ]
Instead I'm getting the error:
test.cpp: In function ‘int main()’:
test.cpp:19: error: no matching function for call to ‘set_intersection(std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>)’
What I understand out of this error, is that there's no definition in set_intersection
that accepts Rb_tree_const_iterator<int>
as parameter.
Furthermore, I suppose the std::set.begin()
method returns an object of such type,
is there a better way to find the intersection of two std::set
in C++? Preferably a built-in function?
Thanks a lot!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…