diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP')
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP new file mode 100644 index 0000000..10df8f5 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP @@ -0,0 +1,94 @@ +// Fig. 21.35: fig21_35.cpp
+// Standard library algorithms includes, set_difference,
+// set_intersection, set_symmetric_difference and set_union.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <algorithm> // algorithm definitions
+
+int main()
+{
+ const int SIZE1 = 10, SIZE2 = 5, SIZE3 = 20;
+ int a1[ SIZE1 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+ int a2[ SIZE2 ] = { 4, 5, 6, 7, 8 };
+ int a3[ SIZE2 ] = { 4, 5, 6, 11, 15 };
+ std::ostream_iterator< int > output( cout, " " );
+
+ cout << "a1 contains: ";
+ std::copy( a1, a1 + SIZE1, output );
+ cout << "\na2 contains: ";
+ std::copy( a2, a2 + SIZE2, output );
+ cout << "\na3 contains: ";
+ std::copy( a3, a3 + SIZE2, output );
+
+ // determine whether set a2 is completely contained in a1
+ if ( std::includes( a1, a1 + SIZE1, a2, a2 + SIZE2 ) )
+ cout << "\n\na1 includes a2";
+ else
+ cout << "\n\na1 does not include a2";
+
+ // determine whether set a3 is completely contained in a1
+ if ( std::includes( a1, a1 + SIZE1, a3, a3 + SIZE2 ) )
+ cout << "\na1 includes a3";
+ else
+ cout << "\na1 does not include a3";
+
+ int difference[ SIZE1 ];
+
+ // determine elements of a1 not in a2
+ int *ptr = std::set_difference( a1, a1 + SIZE1,
+ a2, a2 + SIZE2, difference );
+
+ cout << "\n\nset_difference of a1 and a2 is: ";
+ std::copy( difference, ptr, output );
+
+ int intersection[ SIZE1 ];
+
+ // determine elements in both a1 and a2
+ ptr = std::set_intersection( a1, a1 + SIZE1,
+ a2, a2 + SIZE2, intersection );
+
+ cout << "\n\nset_intersection of a1 and a2 is: ";
+ std::copy( intersection, ptr, output );
+
+ int symmetric_difference[ SIZE1 ];
+
+ // determine elements of a1 that are not in a2 and
+ // elements of a2 that are not in a1
+ ptr = std::set_symmetric_difference( a1, a1 + SIZE1,
+ a2, a2 + SIZE2, symmetric_difference );
+
+ cout << "\n\nset_symmetric_difference of a1 and a2 is: ";
+ std::copy( symmetric_difference, ptr, output );
+
+ int unionSet[ SIZE3 ];
+
+ // determine elements that are in either or both sets
+ ptr = std::set_union( a1, a1 + SIZE1,
+ a3, a3 + SIZE2, unionSet );
+
+ cout << "\n\nset_union of a1 and a3 is: ";
+ std::copy( unionSet, ptr, output );
+
+ cout << endl;
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file |
