Sets and the Set_intersection Function syntax

Hello guys, I have a question about using the set_intersection function in c++. I'm in the process of writing a program that will find the intersections of two sets of numbers or words, and put them into a new set. Getting the values into sets isn't difficult, but using the set_intersection function to make the third set is a real pain. I have outlined what i'm certain is the problem code, but from the text i've read on it, I cant figure out any other way to write this. If anyone has any suggestions, it would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <iterator>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
	string words_one[2] = { "omg", "hey" };
	string words_two[3] = { "hey", "you", "guy" };


	set<string> first(words_one, words_one + 2);
	set<string> second(words_two, words_two + 3);

	set<string> third;
//**********************************************************************
	set<string> ::iterator cIter;
	insert_iterator<set<string> > cIterator(third, third.begin());
	set_intersection(first.begin(), first.end(), 
	second.begin(), second.end(), cIter);
//*************************************************************************	
	set<string>::iterator iter = third.begin();
	while (iter != third.end()) 
	{
		cout << *iter;
		iter++;
	}
	system("pause");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <iterator>
#include <set>
#include <algorithm>

int main()
{
    const std::set<std::string> first { "omg", "hey", "there", "you" };
    const std::set<std::string> second { "hey", "look", "you", "guy" };

    std::set<std::string> third;
    // http://en.cppreference.com/w/cpp/iterator/inserter
    std::set_intersection( first.begin(), first.end(), second.begin(), second.end(), 
                           std::inserter( third, third.end() ) );
                           
    for( const std::string& str : third ) std::cout << str << ' ' ;
    std::cout << '\n' ;
    
    third.clear() ;
    std::set_union( first.begin(), first.end(), second.begin(), second.end(), 
                    std::inserter( third, third.end() ) ); 
                           
    for( const std::string& str : third ) std::cout << str << ' ' ;
    std::cout << '\n' ;
    
    third.clear() ;
    std::set_difference( first.begin(), first.end(), second.begin(), second.end(), 
                         std::inserter( third, third.end() ) );
                           
    for( const std::string& str : third ) std::cout << str << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/ac43f21bf9b7cf02
Love the reference site, don't know how I missed that, thanks a bunch!
Topic archived. No new replies allowed.