multiset(3C++) - multiset(3C++)
Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.
NAMEmultiset
- An associative container that allows fast access to stored key val‐
ues. Storage of duplicate keys is allowed. A multiset supports bidirec‐
tional iterators.
SYNOPSIS
#include <set>
template <class Key, class Compare = less<Key>,
class Allocator = allocator<Key> >
class multiset;
DESCRIPTION
multiset_<Key,_Compare,_Allocator>_allows fast access to stored key
values. The default operation for key comparison is the < 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 Key must include the following
(where T is the type, t is a value of T and u is a const value of T):
Copy constructors T(t) and T(u)
Destructor t.~T()
Address of &t and &u yielding T* and const T* respectively
Assignment t = a where a is a (possibly const) value of T
The type used for the Compare template parameter must satisfy the
requirements for binary functions.
INTERFACE
template <class Key, class Compare = less<Key>,
class Allocator = allocator<Key> >
class multiset{
public:
// typedefs
typedef Key key_type;
typedef Key value_type;
typedef Compare key_compare;
typedef Compare value_compare;
typedef Allocator allocator_type;
typedef typename
Allocator::reference reference;
typedef typename
Allocator::const_reference const_reference;
class iterator;
class const_iterator;
typedef typename
Allocator::size_type size_type;
typedef typename
Allocator::difference_type difference_type;
typedef typename std::reverse_iterator<iterator>
reverse_iterator;
typedef typename std::reverse_iterator<const_iterator>
const_reverse_iterator;
// Construct/Copy/Destroy
explicit multiset (const Compare& = Compare(),
const Allocator& = Allocator());
template <class InputIterator>
multiset (InputIterator, InputIterator,
const Compare& = Compare(),
const Allocator& = Allocator());
multiset (const multiset<Key, Compare, Allocator>&);
~multiset ();
multiset<Key, Compare, Allocator>&
operator= (const multiset<Key,
Compare, Allocator>&);
// Iterators
iterator begin ();
const_iterator begin () const;
iterator end ();
const_iterator end () const;
reverse_iterator rbegin ();
const_reverse_iterator rbegin () const;
reverse_iterator rend ();
const_reverse_iterator rend () const;
// Capacity
bool empty () const;
size_type size () const;
size_type max_size () const;
// Modifiers
iterator insert (const value_type&);
iterator insert (iterator, const value_type&);
template <class InputIterator>
void insert (InputIterator, InputIterator);
void erase (iterator);
size_type erase (const key_type&);
void erase (iterator, iterator);
void swap (multiset<Key, Compare, Allocator>&);
void clear ();
// Observers
key_compare key_comp () const;
value_compare value_comp () const;
// Multiset operations
iterator find (const key_type&) const;
size_type count (const key_type&) const;
iterator lower_bound (const key_type&) const;
iterator upper_bound (const key_type&) const;
pair<iterator, iterator> equal_range
(const key_type&) const;
};
// Non-member Operators
template <class Key, class Compare, class Allocator>
bool operator==
(const multiset<Key, Compare, Allocator>&,
const multiset<Key, Compare, Allocator>&);
template <class Key, class Compare, class Allocator>
bool operator!=
(const multiset<Key, Compare, Allocator>&,
const multiset<Key, Compare, Allocator>&);
template <class Key, class Compare, class Allocator>
bool operator<
(const multiset<Key, Compare, Allocator>&,
const multiset<Key, Compare, Allocator>&);
template <class Key, class Compare, class Allocator>
bool operator>
(const multiset<Key, Compare, Allocator>&,
const multiset<Key, Compare, Allocator>&);
template <class Key, class Compare, class Allocator>
bool operator<=
(const multiset<Key, Compare, Allocator>&,
const multiset<Key, Compare, Allocator>&);
template <class Key, class Compare, class Allocator>
bool operator>=
(const multiset<Key, Compare, Allocator>&,
const multiset<Key, Compare, Allocator>&);
// Specialized Algorithms
template <class Key, class Compare, class Allocator>
void swap ( multiset<Key, Compare, Allocator>&,
multiset<Key, Compare, Allocator>&);
CONSTRUCTORSexplicit multiset(const Compare& comp = Compare(),
const Allocator& alloc = Allocator());
Constructs an empty multiset that uses the optional relation comp to order
keys, if it is supplied, and the allocator alloc for all storage manage‐
ment.
template <class InputIterator>
multiset(InputIterator first, InputIterator last,
const Compare& = Compare(),
const Allocator& = Allocator());
Constructs a multiset containing values in the range [first, last).
multiset(const multiset<Key, Compare, Allocator>& x);
Creates a new multiset by copying all key values from x.
DESTRUCTORS
~multiset();
Releases any allocated memory for this multiset.
ASSIGNMENT OPERATORS
multiset<Key, Compare, Allocator>&
operator=(const multiset<Key, Compare, Allocator>& x);
Replaces the contents of *this with a copy of the contents of x.
ALLOCATORS
allocator_type
get_allocator() const;
Returns a copy of the allocator used by self for storage management.
ITERATORS
iterator
begin();
Returns an iterator pointing to the first element stored in the multiset.
"First" is defined by the multiset's comparison operator, Compare.
const_iteratorbegin();
Returns a const_iterator pointing to the first element stored in the multi‐
set.
iteratorend();
Returns an iterator pointing to the last element stored in the multiset (in
other words, the off-the-end value).
const_iteratorend();
Returns a const_iterator pointing to the last element stored in the multi‐
set (in other words, the off-the-end value).
reverse_iteratorrbegin();
Returns a reverse_iterator pointing to the first element stored in the mul‐
tiset. "First" is defined by the multiset's comparison operator, Compare.
const_reverse_iteratorrbegin();
Returns a const_reverse_iterator pointing to the first element stored in
the multiset.
reverse_iteratorrend();
Returns a reverse_iterator pointing to the last element stored in the mul‐
tiset (in other words, the off-the-end value).
const_reverse_iteratorrend();
Returns a const_reverse_iterator pointing to the last element stored in the
multiset (in other words, the off-the-end value).
MEMBER FUNCTIONS
void
clear();
Erases all elements from the self.
size_typecount(const key_type& x) const;
Returns the number of elements in the multiset with the key value x.
boolempty() const;
Returns true if the multiset is empty, false otherwise.
pair<iterator,iterator>
equal_range(const key_type& x)const;
Returns the pair (lower_bound(x), upper_bound(x)).
size_typeerase(const key_type& x);
Deletes all elements with the key value x from the multiset, if any exist.
Returns the number of deleted elements.
voiderase(iterator position);
Deletes the multiset element pointed to by the iterator position. Returns
an iterator pointing to the element following the deleted element, or
end(), if the deleted item was the last one in this list.
voiderase(iterator first, iterator last);
If the iterators first and last point to the same multiset and last is
reachable from first, all elements in the range (first, last) are deleted
from the multiset. Returns an iterator pointing to the element following
the last deleted element or end(), if there were no elements after the
deleted range.
iteratorfind(const key_type& x) const;
Searches the multiset for a key value x and returns an iterator to that key
if it is found. If such a value is not found, the iterator end() is
returned.
iteratorinsert(const value_type& x);
iteratorinsert(iterator position, const value_type& x);
x 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 O(log
N) time.
template <class InputIterator>
voidinsert(InputIterator first, InputIterator last);
Copies of each element in the range [first, last) are inserted into the
multiset. This insert takes approximately O(N*log(size()+N)) time.
key_comparekey_comp() const;
Returns a function object capable of comparing key values using the compar‐
ison operation, Compare, of the current multiset.
iteratorlower_bound(const key_type& x) const;
Returns an iterator to the first element whose key is greater than or equal
to x. If no such element exists, end() is returned.
size_typemax_size() const;
Returns the maximum possible size of the multiset size_type.
size_typesize() const;
Returns the number of elements in the multiset.
voidswap(multiset<Key, Compare, Allocator>& x);
Swaps the contents of the multiset x with the current multiset, *this.
iteratorupper_bound(const key_type& x) const;
Returns an iterator to the first element whose key is smaller than or equal
to x. If no such element exists, then end() is returned.
value_comparevalue_comp() const;
Returns a function object capable of comparing key values using the compar‐
ison operation, Compare, of the current multiset.
NON-MEMBER OPERATORS
template <class Key, class Compare, class Allocator>
operator==(const multiset<Key, Compare, Allocator>& x,
const multiset<Key, Compare, Allocator>& y);
Returns true if all elements in x are element-wise equal to all elements in
y, using (T::operator==). Otherwise it returns false.
template <class Key, class Compare, class Allocator>
operator!=(const multiset<Key, Compare, Allocator>& x,
const multiset<Key, Compare, Allocator>& y);
Returns !(x==y).
template <class Key, class Compare, class Allocator>
operator<(const multiset<Key, Compare, Allocator>& x,
const multiset<Key, Compare, Allocator>& y);
Returns true if x is lexicographically less than y. Otherwise, it returns
false.
template <class Key, class Compare, class Allocator>
operator>(const multiset<Key, Compare, Allocator>& x,
const multiset<Key, Compare, Allocator>& y);
Returns y < x.
template <class Key, class Compare, class Allocator>
operator<=(const multiset<Key, Compare, Allocator>& x,
const multiset<Key, Compare, Allocator>& y);
Returns !(y < x).
template <class Key, class Compare, class Allocator>
operator>=(const multiset<Key, Compare, Allocator>& x,
const multiset<Key, Compare, Allocator>& y);
Returns !(x < y).
SPECIALIZED ALGORITHMStemplate <class Key, class Compare, class Allocator>
void swap(multiset<Key,Compare,Allocator>& a,
multiset<Key,Compare,Allocator>&b);
Swaps the contents of a and b.
EXAMPLE
//
// multiset.cpp
//
#include <set>
#include <iostream>
using namespace std;
typedef multiset<int, less<int>, allocator> set_type;
ostream& operator<<(ostream& out, const set_type& s)
{
copy(s.begin(),s.end(),
ostream_iterator<set_type::value_type,char>(cout," "));
return out;
}
int main(void)
{
// create a multiset of ints
set_type si;
int i;
for (int j = 0; j < 2; j++)
{
for(i = 0; i < 10; ++i) {
// insert values with a hint
si.insert(si.begin(), i);
}
}
// print out the multiset
cout << si << endl;
// Make another int multiset and an empty multiset
set_type si2, siResult;
for (i = 0; i < 10; i++)
si2.insert(i+5);
cout << si2 << endl;
// Try a couple of set algorithms
set_union(si.begin(),si.end(),si2.begin(),si2.end(),
inserter(siResult,siResult.begin()));
cout << "Union:" << endl << siResult << endl;
siResult.erase(siResult.begin(),siResult.end());
set_intersection(si.begin(),si.end(),
si2.begin(),si2.end(),
inserter(siResult,siResult.begin()));
cout << "Intersection:" << endl << siResult << endl;
return 0;
}
Program Output0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 95 6 7 8 9 10 11 12 13 14Union:0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 11 12 13 14Intersection:5 6 7 8 9WARNINGS
Member function templates are used in all containers included in the
Standard Template Library. An example of this feature is the construc‐
tor for_multiset<Key,_Compare,_Allocator>, which takes two templatized
iterators:
template <class InputIterator>
multiset (InputIterator, InputIterator,
const Compare& = Compare(),
const Allocator& = Allocator());
multiset also has an insert function of this type. These functions, when not
restricted by compiler limitations, allow you to use any type of input itera‐
tor as arguments. For compilers that do not support this feature, substitute
functions allow you to use an iterator obtained from the same type of con‐
tainer 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:int intarray[10];
multiset<int> first_multiset(intarray, intarray +10);
multiset<int> second_multiset(first_multiset.begin(),
first_multiset.end());
but not this way:multiset<long>
long_multiset(first_multiset.begin(),first_multiset.end());
since the long_multiset and first_multiset are not the same type.Also, many compilers do not support default template arguments. If your com‐
piler is one of these you always need to supply the Compare template argumentand the Allocator template argument. For instance, you have to write:
multiset<int, less<int>, allocator<int> >
instead of:multiset<int>
If your compiler does not support namespaces, then you do not need the using
declaration for std.SEE ALSO
allocator, Containers, Iterators, set
Rogue Wave Software 02 Apr 1998 multiset(3C++)