[TOJ 1743] set operation (use of set union, intersection, and difference), toj1743
DescriptionAll elements of two sets A and B are given, and their intersection, union, and difference sets are calculated.InputThere are multiple groups of input data. The first row is T, and the second row is 2. Each group occupies 2 rows. Each row has several integers. All integers in the first row constitute, all integers in the second row constitute the Set B, which are separated by spaces. A and B can have no more than 100 elements.OutputOutput all the elements in the intersection, union, and difference sets of A and B (in ascending order ). Each set occupies one row. data elements are separated by spaces.Sample Input
1
0 1 2 3 4 5 6 7 8
3 6 8 9
Sample Output
3 6 8
0 1 2 3 4 5 6 7 8 9
0 1 2 4 5 7
# Include <set> # include <algorithm> # include <iterator> # include <iostream> # include <sstream> using namespace std; int main () {set <int> v1, v2, v3; string s1, s2; int I, j, t, a; set <int >:: iterator it; cin >>t; getchar (); while (t --) {j = 0; getline (cin, s1); // numeric string import s1 stringstream ss1 (s1 ); // string s1 re-import the while (ss1> a) in the ss1 stream // The string s1 in the stream is imported into int a into a number {v1.insert (); // transfer the number temporarily saved in a to set} getline (cin, s2); stringstream ss2 (s2); while (ss2> a) {v2.insert ();} set_intersection (v1.begin (), v1.end (), v2.begin (), v2.end (), inserter (v3, v3.begin ()));
// Submit for (it = v3.begin (); it! = V3.end (); it ++) {if (j = 0) {cout <* it; j = 1 ;} else cout <"" <* it;} cout <endl; v3.clear (); // clear the set_union (v1.begin (), v1.end (), v2.begin (), v2.end (), inserter (v3, v3.begin ()));
// And j = 0; for (it = v3.begin (); it! = V3.end (); it ++) {if (j = 0) {cout <* it; j = 1 ;} else cout <"" <* it;} cout <endl; v3.clear (); set_difference (v1.begin (), v1.end (), v2.begin (), v2.end (), inserter (v3, v3.begin ()));
// Difference j = 0; for (it = v3.begin (); it! = V3.end (); it ++) {if (j = 0) {cout <* it; j = 1 ;} else cout <"" <* it ;}cout <endl; v3.clear (); v1.clear (); // clear the v1 set v2.clear (); // clear the v2 set} return 0 ;}