.\" ident @(#)multiset.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH multiset 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2multiset\fP \ - An associative container that allows fast access to stored key values. Storage of duplicate keys is allowed. A multiset supports bidirectional iterators. .SH SYNOPSIS .br #include .br template , .RE .RS 9 class Allocator = allocator > .RE .RS 0 class multiset; .SH DESCRIPTION multiset__allows fast access to stored key values. The default operation for key comparison is the \f2<\fP operator. Insertion of duplicate keys is allowed with a multiset. multiset uses bidirectional iterators that point to a stored key. Any type used for the template parameter \f2Key\fP must include the following (where\f2 T\fP is the \f2type\fP,\f2 t\fP is a value of \f2T\fP and\f2 u\fP is a \f2const\fP \f2value\fP of \f2T\fP): .HP 20 Copy constructors \f2T(t)\fP and \f2T(u)\fP .HP 0 .HP 13 Destructor \f2t.~T()\fP .HP 0 .HP 13 Address of \f2&t\fP and \f2&u\fP yielding \f2T*\fP and \f2const T*\fP respectively .HP 0 .HP 13 Assignment \f2t = a \fPwhere \f2a\fP is a (possibly \f2const\fP) value of \f2T\fP .HP 0 The \f2type\fP used for the \f2Compare\fP template parameter must satisfy the requirements for binary functions. .SH INTERFACE .br template , .RE .RS 9 class Allocator = allocator > .RE .RS 0 class multiset { .br .br public: .br .br // typedefs .br .RE .RS 2 typedef Key key_type; .br typedef Key value_type; .br typedef Compare key_compare; .br typedef Compare value_compare; .br typedef Allocator allocator_type; .br typedef typename .RE .RS 10 Allocator::reference reference; .RE .RS 2 typedef typename .RE .RS 10 Allocator::const_reference const_reference; .RE .RS 2 class iterator; .br class const_iterator; .br typedef typename .RE .RS 10 Allocator::size_type size_type; .RE .RS 2 typedef typename .RE .RS 10 Allocator::difference_type difference_type; .RE .RS 2 typedef typename std::reverse_iterator .RE .RS 24 reverse_iterator; .RE .RS 2 typedef typename std::reverse_iterator .RE .RS 24 const_reverse_iterator; .RE .RS 0 .br // Construct/Copy/Destroy .br .RE .RS 2 explicit multiset (const Compare& = Compare(), .RE .RS 21 const Allocator& = Allocator()); .RE .RS 2 template .RE .RS 3 multiset (InputIterator, InputIterator, .RE .RS 13 const Compare& = Compare(), .br const Allocator& = Allocator()); .RE .RS 2 multiset (const multiset&); .RE .RS 3 ~multiset (); .RE .RS 2 multiset& .RE .RS 11 operator= (const multiset&); .RE .RS 0 .br // Iterators .br .RE .RS 2 iterator begin (); .br const_iterator begin () const; .br iterator end (); .br const_iterator end () const; .br reverse_iterator rbegin (); .br const_reverse_iterator rbegin () const; .br reverse_iterator rend (); .br const_reverse_iterator rend () const; .RE .RS 0 .br // Capacity .br .RE .RS 2 bool empty () const; .br size_type size () const; .br size_type max_size () const; .RE .RS 0 .br // Modifiers .br .RE .RS 2 iterator insert (const value_type&); .br iterator insert (iterator, const value_type&); .br template .RE .RS 3 void insert (InputIterator, InputIterator); .RE .RS 0 .RE .RS 2 void erase (iterator); .br size_type erase (const key_type&); .br void erase (iterator, iterator); .br void swap (multiset&); .br void clear (); .RE .RS 0 .br // Observers .br .RE .RS 2 key_compare key_comp () const; .br value_compare value_comp () const; .RE .RS 0 .br // Multiset operations .br .RE .RS 2 iterator find (const key_type&) const; .br size_type count (const key_type&) const; .br iterator lower_bound (const key_type&) const; .br iterator upper_bound (const key_type&) const; .br pair equal_range .RE .RS 7 (const key_type&) const; .RE .RS 3 }; .RE .RS 0 .br // Non-member Operators .br .br template .br bool operator== .RE .RS 4 (const multiset&, .br const multiset&); .RE .RS 0 .br template .br bool operator!= .RE .RS 4 (const multiset&, .br const multiset&); .RE .RS 0 .br template .br bool operator< .RE .RS 4 (const multiset&, .br const multiset&); .RE .RS 0 .br template .br bool operator> .RE .RS 4 (const multiset&, .br const multiset&); .RE .RS 0 .br template .br bool operator<= .RE .RS 4 (const multiset&, .br const multiset&); .RE .RS 0 .br template .br bool operator>= .RE .RS 4 (const multiset&, .br const multiset&); .RE .RS 0 .br // Specialized Algorithms .br .br template .br void swap ( multiset&, .RE .RS 12 multiset&); .SH CONSTRUCTORS .RE .RS 0 explicit multiset(const Compare& comp = Compare(), .RE .RS 18 const Allocator& alloc = Allocator()); .RE .RS 3 Constructs an empty multiset that uses the optional relation \f2comp\fP to order keys, if it is supplied, and the allocator \f2alloc\fP for all storage management. .RE .RE .RS 0 template .br multiset(InputIterator first, InputIterator last, .RE .RS 9 const Compare& = Compare(), .br const Allocator& = Allocator()); .RE .RS 3 Constructs a multiset containing values in the range \f2[first, last).\fP .RE .RE .RS 0 multiset(const multiset& x); .RE .RS 3 Creates a new multiset by copying all key values from \f2x\fP. .RE .SH DESTRUCTORS .br ~multiset(); .RE .RS 3 Releases any allocated memory for this multiset. .RE .SH ASSIGNMENT OPERATORS .br multiset& .br operator=(const multiset& x); .RE .RS 3 Replaces the contents of \f2*this\fP with a copy of the contents of \f2x\fP. .RE .SH ALLOCATORS .br allocator_type .br get_allocator() const; .RE .RS 3 Returns a copy of the allocator used by self for storage management. .RE .SH ITERATORS .br iterator .br begin(); .RE .RS 3 Returns an \f2iterator\fP pointing to the first element stored in the multiset. "First" is defined by the multiset's comparison operator, \f2Compare\fP. .RE .br const_iterator .br begin(); .RE .RS 3 Returns a \f2const_iterator\fP pointing to the first element stored in the multiset. .RE .br iterator .br end(); .RE .RS 3 Returns an \f2iterator\fP pointing to the last element stored in the multiset (in other words, the off-the-end value). .RE .br const_iterator .br end(); .RE .RS 3 Returns a \f2const_iterator\fP pointing to the last element stored in the multiset (in other words, the off-the-end value). .RE .br reverse_iterator .br rbegin(); .RE .RS 3 Returns a \f2reverse_iterator\fP pointing to the first element stored in the multiset. "First" is defined by the multiset's comparison operator, \f2Compare\fP. .RE .br const_reverse_iterator .br rbegin(); .RE .RS 3 Returns a \f2const_reverse_iterator\fP pointing to the first element stored in the multiset. .RE .br reverse_iterator .br rend(); .RE .RS 3 Returns a \f2reverse_iterator\fP pointing to the last element stored in the multiset (in other words, the off-the-end value). .RE .br const_reverse_iterator .br rend(); .RE .RS 3 Returns a \f2const_reverse_iterator\fP pointing to the last element stored in the multiset (in other words, the off-the-end value). .RE .SH MEMBER FUNCTIONS .br void .br clear(); .RE .RS 3 Erases all elements from the self. .RE .br size_type .br count(const key_type& x) const; .RE .RS 3 Returns the number of elements in the multiset with the key value \f2x\fP. .RE .br bool .br empty() const; .RE .RS 3 Returns \f2true\fP if the multiset is empty, \f2false\fP otherwise. .RE .br pair .br equal_range(const key_type& x)const; .RE .RS 3 Returns the pair \f2(lower_bound(x), upper_bound(x))\fP. .RE .br size_type .br erase(const key_type& x); .RE .RS 3 Deletes all elements with the key value \f2x\fP from the multiset, if any exist. Returns the number of deleted elements. .RE .br void .br erase(iterator position); .RE .RS 3 Deletes the multiset element pointed to by the iterator \f2position\fP. Returns an iterator pointing to the element following the deleted element, or \f2end()\fP, if the deleted item was the last one in this list. .RE .br void .br erase(iterator first, iterator last); .RE .RS 3 If the iterators \f2first\fP and \f2last\fP point to the same multiset and last is reachable from first, all elements in the range (\f2first, last\fP) are deleted from the multiset. Returns an iterator pointing to the element following the last deleted element or \f2end()\fP, if there were no elements after the deleted range. .RE .br iterator .br find(const key_type& x) const; .RE .RS 3 Searches the multiset for a key value \f2x\fP and returns an iterator to that key if it is found. If such a value is not found, the iterator \f2end()\fP is returned. .RE .br iterator .br insert(const value_type& x); .br iterator .br insert(iterator position, const value_type& x); .RE .RS 3 \f2x\fP is inserted into the multiset. A position may be supplied as a hint regarding where to do the insertion. If the insertion is done right after position, then it takes amortized constant time. Otherwise, it takes \f2O(log N) \fPtime. .RE .br template .br void .br insert(InputIterator first, InputIterator last); .RE .RS 3 Copies of each element in the range \f2[first, last)\fP are inserted into the multiset. This \f2insert\fP takes approximately \f2O(N*log(size()+N))\fP time. .RE .br key_compare .br key_comp() const; .RE .RS 3 Returns a function object capable of comparing key values using the comparison operation, \f2Compare\fP, of the current multiset. .RE .br iterator .br lower_bound(const key_type& x) const; .RE .RS 3 Returns an iterator to the first element whose key is greater than or equal to \f2x\fP. If no such element exists, \f2end()\fP is returned. .RE .br size_type .br max_size() const; .RE .RS 3 Returns the maximum possible size of the multiset \f2size_type.\fP .RE .br size_type .br size() const; .RE .RS 3 Returns the number of elements in the multiset. .RE .br void .br swap(multiset& x); .RE .RS 3 Swaps the contents of the multiset \f2x\fP with the current multiset, \f2*this\fP. .RE .br iterator .br upper_bound(const key_type& x) const; .RE .RS 3 Returns an iterator to the first element whose key is smaller than or equal to \f2x.\fP If no such element exists, then \f2end() \fPis returned. .RE .br value_compare .br value_comp() const; .RE .RS 3 Returns a function object capable of comparing key values using the comparison operation, \f2Compare\fP, of the current multiset. .RE .SH NON-MEMBER OPERATORS .br template .br operator==(const multiset& x, .RE .RS 11 const multiset& y); .RE .RS 3 Returns \f2true\fP if all elements in \f2x\fP are element-wise equal to all elements in \f2y\fP, using \f2(T::operator==)\fP. Otherwise it returns \f2false\fP. .RE .RE .RS 0 template .br operator!=(const multiset& x, .RE .RS 11 const multiset& y); .RE .RS 3 Returns \f2!(x==y)\fP. .RE .RE .RS 0 template .br operator<(const multiset& x, .RE .RS 10 const multiset& y); .RE .RS 3 Returns \f2true\fP if \f2x\fP is lexicographically less than \f2y\fP. Otherwise, it returns \f2false\fP. .RE .RE .RS 0 template .br operator>(const multiset& x, .RE .RS 10 const multiset& y); .RE .RS 3 Returns \f2y < x\fP. .RE .RE .RS 0 template .br operator<=(const multiset& x, .RE .RS 10 const multiset& y); .RE .RS 3 Returns \f2!(y < x)\fP. .RE .RE .RS 0 template .br operator>=(const multiset& x, .RE .RS 10 const multiset& y); .RE .RS 3 Returns \f2!(x < y)\fP. .RE .SH SPECIALIZED ALGORITHMS .RE .RS 0 template .br void swap(multiset& a, .RE .RS 10 multiset&b); .RE .RS 3 Swaps the contents of \f2a\fP and \f2b\fP. .RE .SH EXAMPLE .RE .RS 0 // .br // multiset.cpp .br // .br #include .br #include .br using namespace std; .br .br typedef multiset, allocator> set_type; .br .br ostream& operator<<(ostream& out, const set_type& s) .RE .RS 1 { .RE .RS 2 copy(s.begin(),s.end(), .RE .RS 4 ostream_iterator(cout," ")); .RE .RS 2 return out; .RE .RS 1 } .RE .RS 0 .br .br int main(void) .RE .RS 1 { .RE .RS 3 // create a multiset of ints .RE .RS 2 set_type si; .br int i; .RE .RS 0 .RE .RS 2 for (int j = 0; j < 2; j++) .RE .RS 3 { .RE .RS 4 for(i = 0; i < 10; ++i) { .RE .RS 7 // insert values with a hint .RE .RS 6 si.insert(si.begin(), i); .RE .RS 5 } .RE .RS 3 } .RE .RS 0 .RE .RS 3 // print out the multiset .RE .RS 2 cout << si << endl; .RE .RS 0 .RE .RS 3 // Make another int multiset and an empty multiset .RE .RS 2 set_type si2, siResult; .br for (i = 0; i < 10; i++) .RE .RS 5 si2.insert(i+5); .RE .RS 2 cout << si2 << endl; .RE .RS 0 .RE .RS 3 // Try a couple of set algorithms .RE .RS 2 set_union(si.begin(),si.end(),si2.begin(),si2.end(), .RE .RS 9 inserter(siResult,siResult.begin())); .RE .RS 2 cout << "Union:" << endl << siResult << endl; .RE .RS 0 .RE .RS 2 siResult.erase(siResult.begin(),siResult.end()); .br set_intersection(si.begin(),si.end(), .RE .RS 9 si2.begin(),si2.end(), .br inserter(siResult,siResult.begin())); .RE .RS 2 cout << "Intersection:" << endl << siResult << endl; .br .br return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 .br 5 6 7 8 9 10 11 12 13 14 .br Union: .br 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 11 12 13 14 .br Intersection: .br 5 6 7 8 9 .SH WARNINGS Member function templates are used in all containers included in the Standard Template Library. An example of this feature is the constructor for_multiset, which takes two templatized iterators: .br template .br multiset (InputIterator, InputIterator, .RE .RS 9 const Compare& = Compare(), .br const Allocator& = Allocator()); .RE multiset also has an \f2insert\fP function of this type. These functions, when not restricted by compiler limitations, allow you to use any type of input iterator as arguments. For compilers that do not support this feature, substitute functions allow you to use an iterator obtained from the same type of container as the one you are constructing (or calling a member function on). You can also use a pointer to the type of element you have in the container. For example, if your compiler does not support member function templates, you can construct a multiset in the following two ways: .RE .RS 0 int intarray[10]; .br multiset first_multiset(intarray, intarray +10); .br multiset second_multiset(first_multiset.begin(), .RE .RS 13 first_multiset.end()); .RE but not this way: .RE .RS 0 \f2multiset\fP .br long_multiset(first_multiset.begin(),first_multiset.end()); since the \f2long_multiset\fP and\f2 first_multiset \fPare not the same type. Also, many compilers do not support default template arguments. If your compiler is one of these you always need to supply the \f2Compare\fP template argument and the \f2Allocator\fP template argument. For instance, you have to write: \f2multiset, allocator >\fP instead of: \f2multiset\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO allocator, Containers, Iterators, set