Examples of STL set_intersection

Examples using vector (should be ordered) and set as containers.

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main (){

    int ra[ ] = {1, 2, 4, 6, 8, 10, 12};
    int rb[ ] = {1, 3, 6, 9, 12, 15, 18};
    vector<int> a(ra, ra+7);
    vector<int> b(rb, rb+7);
    vector<int> c;

    set_intersection(a.begin(), a.end(), 
                     b.begin(), b.end(),
                     back_inserter(c));

    for(int i=0; i<c.size(); i++)
        cout << c[i] << " ";
    cout << endl;

    set<int> sa(ra, ra+7);
    set<int> sb(rb, rb+7);
    set<int> sc;
    
    set_intersection(sa.begin(), sa.end(), 
                     sb.begin(), sb.end(),
                     inserter(sc, sc.end()));

    for(set<int>::iterator it=sc.begin(); it != sc.end(); it++)
        cout << *it << " ";
    cout << endl;

}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s