Top of document
©Copyright 1999 Rogue Wave Software

set


     Container

Summary

An associative container that supports unique keys. A set supports bidirectional iterators.

Contents

Synopsis

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

Description

set<Key, Compare, Allocator> is an associative container that supports unique keys and provides for fast retrieval of the keys. A set contains at most one of any key value. The keys are sorted using Compare.

Since a set maintains a total order on its elements, you cannot alter the key values directly. Instead, you must insert new elements with an insert_iterator.

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
                       (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>
 class set {
public:
 // types
   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 set (const Compare& = Compare(), 
                 const Allocator& = Allocator ());
   template <class InputIterator>
    set (InputIterator, InputIterator, const Compare& = Compare(),
         const Allocator& = Allocator ());
   set (const set<Key, Compare, Allocator>&);
   ~set ();
   set<Key, Compare, Allocator>& operator= (const set <Key, Compare, 
                                                       Allocator>&);
   allocator_type get_allocator () const;
 // 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
   pair<iterator, bool> 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 (set<Key, Compare, Allocator>&);
   void clear ();
 // Observers
   key_compare key_comp () const;
   value_compare value_comp () const;
 // Set operations
   size_type count (const key_type&) const;
   pair<iterator, iterator> equal_range (const  key_type&) const;
   iterator find (const key_type&) const;
   iterator lower_bound (const key_type&) const;
   iterator upper_bound (const key_type&) const
};
 // Non-member Operators
template <class Key, class Compare, class Allocator>
 bool operator== (const set<Key, Compare, Allocator>&,
                  const set<Key, Compare, Allocator>&);
template <class Key, class Compare, class Allocator>
 bool operator< (const set<Key, Compare, Allocator>&,
                 const set<Key, Compare, Allocator>&);
// Specialized Algorithms
template <class Key, class Compare, class Allocator>
void swap (set <Key, Compare, Allocator>&,
           set <Key, Compare, Allocator>&);

Constructors and Destructors

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

Assignment Operator

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

Allocator

allocator_type get_allocator () const;

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;

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_value& x) const;
pair<iterator, bool> 
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 (set<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>
 bool operator== (const set<Key, Compare, Allocator>& x,
                  const set<Key, Compare, Allocator>& y);
template <class Key, class Compare, class Allocator>
bool operator< (const set <Key, Compare, Allocator>& x,
                const set <Key, Compare, Allocator>& y);
template <class Key, class Compare, class Allocator>
void swap (set <Key, Compare, Allocator>& a,
           set <Key, Compare, Allocator>& b);

Example

//
// set.cpp
//
 #include <set>
 #include <iostream.h>
 typedef set<double, less<double>, 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 set of doubles
   set_type   sd;
   int         i;
   for(i = 0; i < 10; ++i) {
     // insert values
     sd.insert(i);
   }
   // print out the set
   cout << sd << endl << endl;
   // now let's erase half of the elements in the set
   int half = sd.size() >> 1;
   set_type::iterator sdi = sd.begin();
   advance(sdi,half);
   sd.erase(sd.begin(),sdi);
   // print it out again
   cout << sd << endl << endl;
   // Make another set and an empty result set
   set_type sd2, sdResult;
   for (i = 1; i < 9; i++)
      sd2.insert(i+5);
   cout << sd2 << endl;
   // Try a couple of set algorithms
   set_union(sd.begin(),sd.end(),sd2.begin(),sd2.end(),
             inserter(sdResult,sdResult.begin()));
   cout << "Union:" << endl << sdResult << endl;
   sdResult.erase(sdResult.begin(),sdResult.end());
   set_intersection(sd.begin(),sd.end(),
                    sd2.begin(),sd2.end(),
                    inserter(sdResult,sdResult.begin()));
   cout << "Intersection:" << endl << sdResult << endl;
  
   return 0;
 }
Output :
0 1 2 3 4 5 6 7 8 9
5 6 7 8 9
6 7 8 9 10 11 12 13
Union:
5 6 7 8 9 10 11 12 13
Intersection:
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 set <Key, Compare, Allocator> that takes two templated iterators:

template <class InputIterator>
 set (InputIterator, InputIterator,
      const Compare& = Compare(),
      const Allocator& = Allocator());

set 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), or you can 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 set in the following two ways:

int intarray[10];
set<int, less<int>, allocator> first_set(intarray, intarray + 10);
set<int, less<int>, allocator> second_set(first_set.begin(),
                                          first_set.end());

but not this way:

set<long, less<long>, allocator> long_set(first_set.begin(),
                                          first_set.end());

since the long_set and first_set 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 need to write :

set<int, less<int>, allocator>

instead of :

set<int>

See Also

allocator, Bidirectional Iterators, Containers, lexicographical_compare


Top of document