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_19.CPP | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP')
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP new file mode 100644 index 0000000..8b3e890 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP @@ -0,0 +1,90 @@ +// Fig. 21.19: fig21_19.cpp
+// Testing Standard Library class multiset
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <set> // multiset class-template definition
+
+// define short name for multiset type used in this program
+typedef std::multiset< int, std::less< int > > ims;
+
+#include <algorithm> // copy algorithm
+
+int main()
+{
+ const int SIZE = 10;
+ int a[ SIZE ] = { 7, 22, 9, 1, 18, 30, 100, 22, 85, 13 };
+
+ ims intMultiset; // ims is typedef for "integer multiset"
+ std::ostream_iterator< int > output( cout, " " );
+
+ cout << "There are currently " << intMultiset.count( 15 )
+ << " values of 15 in the multiset\n";
+
+ intMultiset.insert( 15 ); // insert 15 in intMultiset
+ intMultiset.insert( 15 ); // insert 15 in intMultiset
+
+ cout << "After inserts, there are "
+ << intMultiset.count( 15 )
+ << " values of 15 in the multiset\n\n";
+
+ // iterator that cannot be used to change element values
+ ims::const_iterator result;
+
+ // find 15 in intMultiset; find returns iterator
+ result = intMultiset.find( 15 );
+
+ if ( result != intMultiset.end() ) // if iterator not at end
+ cout << "Found value 15\n"; // found search value 15
+
+ // find 20 in intMultiset; find returns iterator
+ result = intMultiset.find( 20 );
+
+ if ( result == intMultiset.end() ) // will be true hence
+ cout << "Did not find value 20\n"; // did not find 20
+
+ // insert elements of array a into intMultiset
+ intMultiset.insert( a, a + SIZE );
+
+ cout << "\nAfter insert, intMultiset contains:\n";
+ std::copy( intMultiset.begin(), intMultiset.end(), output );
+
+ // determine lower and upper bound of 22 in intMultiset
+ cout << "\n\nLower bound of 22: "
+ << *( intMultiset.lower_bound( 22 ) );
+ cout << "\nUpper bound of 22: "
+ << *( intMultiset.upper_bound( 22 ) );
+
+ // p represents pair of const_iterators
+ std::pair< ims::const_iterator, ims::const_iterator > p;
+
+ // use equal_range to determine lower and upper bound
+ // of 22 in intMultiset
+ p = intMultiset.equal_range( 22 );
+
+ cout << "\n\nequal_range of 22:"
+ << "\n Lower bound: " << *( p.first )
+ << "\n Upper bound: " << *( p.second );
+
+ 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 |
