Top of document
©Copyright 1999 Rogue Wave Software

multiset


     Container Class

Summary

An associative container providing fast access to stored key values. Storage of duplicate keys is allowed. A multiset supports bidirectional iterators.

Contents

Synopsis

#include <set>
template <class Key, class Compare = less<Key>,
          class Allocator = allocator> 
class multiset;

Description

multiset <Key, Compare, Allocator> provides fast access to stored key values. The default operation for key comparison is the < operator. Insertion of dupliate keys is allowed with a multiset.

multiset provides bidirectional iterators which point to a stored key.

Any type used for the template parameter Key must provide 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
                       (possibley 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>
 class multiset {

public:
// typedefs
   typedef Key key_type;
   typedef Key value_type;
   typedef Compare key_compare;
   typedef Compare value_compare;
   typedef Allocator allocator_type;
   typename reference;
   typename const_reference;
   typename iterator;
   typename const_iterator;
   typename size_type;
   typename difference_type;
   typename reverse_iterator;
   typename 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);
   iterator erase (iterator);
   size_type erase (const key_type&);
   iterator 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>&);
// Specialized Algorithms
template <class Key, class Compare, class Allocator>
 void swap ( multiset<Key, Compare, Allocator>&,
             multiset<Key, Compare, Allocator>&);

Constructor and Destructor

explicit multiset (const Compare& comp = Compare(),
                   const Allocator& alloc = Allocator());
template <class InputIterator>
multiset (InputIterator first, InputIterator last,
          const Compare& = Compare(),
          const Allocator& = Allocator());
multiset (const multiset<Key, Compare, Allocator>& x);
~multiset ();

Assignment Operator

multiset<Key, Compare, Allocator>&  
operator= (const multiset<Key, Compare, Allocator>& x);

Allocator

allocator_type get_allocator () const;

Iterators

iterator begin();
const_iterator begin();
iterator end();
const_iterator end();
reverse_iterator rbegin();
const_reverse_iterator rbegin();
reverse_iterator rend();
const_reverse_iterator rend();

Member Functions

void
clear ();
size_type 
count (const key_type& x) const;
bool 
empty () const;
pair<iterator,iterator> 
equal_range (const key_type& x)const;
size_type 
erase (const key_type& x);
iterator
erase (iterator position);
iterator
erase (iterator first, iterator last);
iterator 
find (const key_type& x) const;
iterator 
insert (const value_type& x);
iterator 
insert (iterator position, const value_type& x);
template <class InputIterator>
void 
insert (InputIterator first, InputIterator last);
key_compare 
key_comp () const;
iterator 
lower_bound (const key_type& x) const;
size_type 
max_size () const;
size_type
size () const;
void 
swap (multiset<Key, Compare, Allocator>& x);
iterator 
upper_bound (const key_type& x) const;
value_compare 
value_comp () const;

Non-member Operators

template <class Key, class Compare, class Allocator>
operator== (const multiset<Key, Compare, Allocator>& x,
            const multiset<Key, Compare, Allocator>& y);
template <class Key, class Compare, class Allocator>
operator< (const multiset<Key, Compare, Allocator>& x,
           const multiset<Key, Compare, Allocator>& y);
template <class Key, class Compare, class Allocator>
void swap (multiset<Key,Compare,Allocator>& a,
           multiset<Key,Compare,Allocator>&b);

Example

//
// multiset.cpp
//
#include <set>
#iclude <iostream.h>
 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>(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;
 }
Output: 
0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
5 6 7 8 9 10 11 12 13 14
Union:
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
Intersection:
5 6 7 8 9

Warnings

Member function templates are used in all containers provided by the Standard Template Library. An example of this feature is the constructor for multiset<Key, Compare, Allocator>, which takes two templated 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 iterator as arguments. For compilers that do not support this feature, we provide substitute functions that 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:

int intarray[10];
multiset<int, less<int>, allocator> first_multiset(intarray, 
                                                   intarray +10);
multiset<int, less<int> , allocator>
   second_multiset(first_multiset.begin(), first_multiset.end());

but not this way:

multiset<long, less<long>, allocator>
   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 compiler is one of these you need to always supply the Compare template argument and the Allocator template argument. For instance, you'll have to write:

multiset<int, less<int>, allocator>

instead of:

multiset<int>

See Also

allocator, Containers, Iterators, set


Top of document