[syntax="cpp"]
#include <set>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
int main()
{
set<string> firstSet;
set<string> secondSet;
set<string> intersection;
string a("first");
string b("second");
string c("third");
firstSet.insert(a);
firstSet.insert(b);
secondSet.insert(b);
secondSet.insert(c);
set<string>::iterator firstIt, secondIt;
for (firstIt = firstSet.begin(); firstIt != firstSet.end(); ++firstIt) cout << *firstIt << ' ';
cout << endl;
for (secondIt = secondSet.begin(); secondIt != secondSet.end(); ++secondIt) cout << *secondIt << ' ';
cout << endl;
set<string>::iterator firstEndIt = firstSet.end();
set<string>::iterator secondEndIt = secondSet.end();
insert_iterator<set<string> > intersectionIt(intersection, intersection.begin());
set_intersection(firstIt, firstEndIt, secondIt, secondEndIt, intersectionIt);
for (set<string>::iterator interIt = intersection.begin(); interIt != intersection.end(); ++interIt) cout << *interIt << ' ';
cout << endl;
return 0;
}
[/syntax]
The output I get is this:
first second
second third
I don't understand why the intersection set is empty. Both firstSet and secondSet contain the string "second", so why doesn't this get put into intersection by the set_intersection function?
Thanks to anyone who can clarify for me!
