stlにおけるコンテナsetクラスの基礎演算交差およびセット差セット(2)
2512 ワード
次のコードは、集合の交差および集合差の演算テストです.
// stlset2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <set>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
set<int> myset1;
set<int> myset2;
set<int> myset3;
set<int> myset4;
set<int> myset5;
set<int> myset6;
printf("myset1:
");
// 0 1 2 3 4 5 6 7 8 9
pair<set<int>::iterator, bool> insert_pair;
for(int i=0; i<10; i++)
{
insert_pair = myset1.insert(i);
}
copy(myset1.begin(), myset1.end(), ostream_iterator<int>(cout, " "));
printf("
");
// 5 6 7 8 9 10 11 12 13 14
for(int j=5; j<15;j++)
{
insert_pair = myset2.insert(j);
}
printf("myset2:
");
copy(myset2.begin(), myset2.end(), ostream_iterator<int>(cout, " "));
printf("
");
printf("union result myset3:
");
//
set_union(myset1.begin(), myset1.end(), myset2.begin(), myset2.end(), inserter(myset3, myset3.begin()));
copy(myset3.begin(), myset3.end(), ostream_iterator<int>(cout, " "));
printf("
");
printf("intersection result myset4:
");
//
set_intersection(myset1.begin(), myset1.end(), myset2.begin(), myset2.end(), inserter(myset4, myset4.begin()));
copy(myset4.begin(), myset4.end(), ostream_iterator<int>(cout, " "));
printf("
");
printf("difference result myset5:
");
//
set_difference(myset1.begin(), myset1.end(), myset2.begin(), myset2.end(), inserter(myset5, myset5.begin()));
copy(myset5.begin(), myset5.end(), ostream_iterator<int>(cout, " "));
printf("
");
printf("symmetric_difference result myset6:
");
//
set_symmetric_difference(myset1.begin(), myset1.end(), myset2.begin(), myset2.end(), inserter(myset6, myset6.begin()));
copy(myset6.begin(), myset6.end(), ostream_iterator<int>(cout, " "));
getchar();
return 0;
}