1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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. *
*************************************************************************/
|