.\" ident @(#)set.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH set 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2set\fP \ - An associative container that supports unique keys. A set supports bidirectional iterators. .SH SYNOPSIS .RE .RS 0 #include .br template , .br class Allocator = allocator > .br class set ; .SH DESCRIPTION set_is an associative container that supports unique keys and allows for fast retrieval of the keys. A set contains, at most, one of any key value. The keys are sorted using \f2Compare\fP. 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\f2 insert_iterator\fP. Any type used for the template parameter \f2Key\fP must include the following (where \f2T\fP is the \f2type\fP, \f2t\fP is a \f2value\fP of \f2T\fP and \f2u\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\fP where \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 , .br class Allocator = allocator > .br class set { .br public: .br // types .br 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 Allocator::reference reference; .br typedef typename Allocator::const_reference const_reference; .br class iterator; .br class const_iterator; .br typedef typename Allocator::size_type size_type; .br typedef typename Allocator::difference_type difference_type; .br typedef typename std::reverse_iterator .RE .RS 21 reverse_iterator; .RE .RS 0 typedef typename std::reverse_iterator .RE .RS 21 const_reverse_iterator; .RE .RS 0 .br // Construct/Copy/Destroy .br explicit set (const Compare& = Compare(), .RE .RS 13 const Allocator& = Allocator ()); .RE .RS 0 template .br set (InputIterator, InputIterator, .RE .RS 4 const Compare& = Compare(), .br const Allocator& = Allocator ()); .RE .RS 0 set (const set&); .br ~set (); .br set& operator= .RE .RS 3 (const set &); .RE .RS 0 allocator_type get_allocator () const; .br .br // Iterators .br 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; .br .br // Capacity .br bool empty () const; .br size_type size () const; .br size_type max_size () const; .br .br // Modifiers .br pair insert (const value_type&); .br iterator insert (iterator, const value_type&); .br template .br void insert (InputIterator, InputIterator); .br void erase (iterator); .br size_type erase (const key_type&); .br void erase (iterator, iterator); .br void swap (set&); .br void clear (); .br .br .br // Observers .br key_compare key_comp () const; .br value_compare value_comp () const; .br .br // Set operations .br size_type count (const key_type&) const; .br pair equal_range (const key_type&) const; .br iterator find (const key_type&) const; .br iterator lower_bound (const key_type&) const; .br iterator upper_bound (const key_type&) const .br }; .br .br // Non-member Operators .br template .br bool operator== (const set&, .br const set&); .br template .br bool operator!= (const set&, .br const set&); .br template .br bool operator< (const set&, .br const set&); .br template .br bool operator> (const set&, .br const set&); .br template .br bool operator<= (const set&, .br const set&); .br template .br bool operator>= (const set&, .br const set&); .br .br // Specialized Algorithms .br template .br void swap (set &, .br set &); .SH CONSTRUCTORS .br explicit .br set(const Compare& comp = Compare(), .br const Allocator& alloc = Allocator()); .RE .RS 3 Creates a set of zero elements. If the function object \f2comp\fP is supplied, it is used to compare elements of the set. Otherwise, the default function object in the template argument is used. The template argument defaults to \f2less (<)\fP. The allocator alloc is used for all storage management. .RE .br template .br set(InputIterator first, InputIterator last, .br const Compare& comp = Compare() .br const Allocator& alloc = Allocator()); .RE .RS 3 Creates a set of length \f2last - first\fP, filled with all values obtained by dereferencing the \f2InputIterators\fP on the range \f2[first, last)\fP. If the function object \f2comp\fP is supplied, it is used to compare elements of the set. Otherwise, the default function object in the template argument is used. The template argument defaults to \f2less (<)\fP. Uses the allocator Allocator() for all storage management. .RE .br set(const set& x); .RE .RS 3 Copy constructor. Creates a copy of \f2x\fP. .RE .SH DESTRUCTORS .br ~set(); .RE .RS 3 Releases any allocated memory for self. .RE .SH ASSIGNMENT OPERATORS .br set& .br operator=(const set& x); .RE .RS 3 Returns a reference to self. Self shares an implementation with \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 that points to the first element in self. .RE .br const_iterator .br begin() const; .RE .RS 3 Returns a \f2const_iterator\fP that points to the first element in self. .RE .br iterator .br end(); .RE .RS 3 Returns an \f2iterator\fP that points to the past-the-end value. .RE .br const_iterator .br end() const; .RE .RS 3 Returns a \f2const_iterator\fP that points to the past-the-end value. .RE .br reverse_iterator .br rbegin(); .RE .RS 3 Returns a \f2reverse_iterator\fP that points to the past-the-end value. .RE .br const_reverse_iterator .br rbegin() const; .RE .RS 3 Returns a \f2const_reverse_iterator\fP that points to the past-the-end value. .RE .br reverse_iterator .br rend(); .RE .RS 3 Returns a \f2reverse_iterator\fP that points to the first element. .RE .br const_reverse_iterator .br rend() const; .RE .RS 3 Returns a \f2const_reverse_iterator\fP that points to the first element. .RE .SH MEMBER FUNCTIONS .br void .br clear(); .RE .RS 3 Erases all elements from the set. .RE .br size_type .br count(const key_type& x) const; .RE .RS 3 Returns the number of elements equal to \f2x\fP. Since a set supports unique keys, \f2count\fP always returns \f21\fP or \f20\fP. .RE .br bool .br empty() const; .RE .RS 3 Returns \f2true\fP if the size is zero. .RE .br pair .br equal_range(const key_type& x) const; .RE .RS 3 Returns \f2pair(lower_bound(x),upper_bound(x))\fP. The \f2equal_range\fP function indicates the valid range for insertion of \f2x\fP into the set. .RE .br size_type .br erase(const key_type& x); .RE .RS 3 Deletes all the elements matching \f2x\fP. Returns the number of elements erased. Since a set supports unique keys, \f2erase\fP always returns \f21\fP or \f20\fP. .RE .br void .br erase(iterator position); .RE .RS 3 Deletes the map element pointed to by the iterator \f2position\fP. Returns an \f2iterator\fP 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 Deletes the elements in the range (\f2first, last\fP). Returns an \f2iterator\fP 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_value& x) const; .RE .RS 3 Returns an \f2iterator\fP that points to the element equal to \f2x\fP. If there is no such element, the iterator points to the past-the-end value. .RE .br pair .br insert(const value_type& x); .RE .RS 3 Inserts \f2x\fP into self according to the comparison function object. The template's default comparison function object is \f2less (<)\fP. If the insertion succeeds, it returns a pair composed of the iterator position where the insertion took place and \f2true\fP. Otherwise, the pair contains the end value and \f2false\fP. .RE .br iterator .br insert(iterator position, const value_type& x); .RE .RS 3 \f2x\fP is inserted into the set. A position may be supplied as a hint regarding where to do the insertion. If the insertion is done right after \f2position\fP, then it takes amortized constant time. Otherwise it takes \f20 (log N)\fP time. The return value points to the inserted \f2x\fP. .RE .br template .br void .br insert(InputIterator first, InputIterator last); .RE .RS 3 Inserts copies of the elements in the range \f2[first, last]\fP. .RE .br key_compare .br key_comp() const; .RE .RS 3 Returns the comparison function object for the set. .RE .br iterator .br lower_bound(const key_type& x) const; .RE .RS 3 Returns an \f2iterator\fP that points to the first element that is greater than or equal to \f2x\fP. If there is no such element, the iterator points to the past-the-end value. .RE .br size_type .br max_size() const; .RE .RS 3 Returns the size of the largest possible set. .RE .br size_type .br size() const; .RE .RS 3 Returns the number of elements. .RE .br void .br swap(set& x); .RE .RS 3 Exchanges self with \f2x\fP. .RE .br iterator .br upper_bound(const key_type& x) const .RE .RS 3 Returns an iterator that points to the first element that is greater than or equal to \f2x\fP. If there is no such element, the iterator points to the past-the-end value. .RE .br value_compare .br value_comp() const; .RE .RS 3 Returns the set's comparison object. This is identical to the function \f2key_comp()\fP. .RE .SH NON-MEMBER OPERATORS .br template .br bool operator==(const set& x, .br const set& y); .RE .RS 3 Returns \f2true\fP if \f2x\fP is the same as \f2y\fP. .RE .br template .br bool operator!=(const set& x, .br const set& y); .RE .RS 3 Returns \f2!(x==y)\fP. .RE .br template .br bool operator<(const set & x, .br const set & y); .RE .RS 3 Returns \f2true\fP if the elements contained in \f2x\fP are lexicographically less than the elements contained in \f2y\fP. .RE .br template .br bool operator>(const set & x, .br const set & y); .RE .RS 3 Returns \f2y < x\fP. .RE .br template .br bool operator<=(const set & x, .br const set & y); .RE .RS 3 Returns \f2!(y < x)\fP. .RE .br template .br bool operator>=(const set & x, .br const set & y); .RE .RS 3 Returns \f2!(x < y)\fP. .RE .SH SPECIALIZED ALGORITHMS .br template .br void swap(set & a, .br set & b); .RE .RS 3 Swaps the contents of \f2a\fP and \f2b\fP. .RE .SH EXAMPLE .br // .br // setex.cpp .br // .br #include .br #include .br using namespace std; .br .br typedef set, allocator > .RE .RS 7 set_type; .RE .RS 0 ostream& operator<<(ostream& out, const set_type& s) .RE .RS 1 { .RE .RS 0 copy(s.begin(), s.end(), .RE .RS 4 ostream_iterator(cout," ")); .RE .RS 0 return out; .RE .RS 1 } .RE .RS 0 .br int main(void) .RE .RS 1 { .RE .RS 0 // create a set of doubles .RE .RS 2 set_type sd; .br int i; .RE .RS 0 .br for(i = 0; i < 10; ++i) { .br // insert values .br sd.insert(i); .RE .RS 3 } .RE .RS 0 .br // print out the set .br cout << sd << endl << endl; .br .br // now let's erase half of the elements in the set .br int half = sd.size() >> 1; .br set_type::iterator sdi = sd.begin(); .br advance(sdi,half); .br sd.erase(sd.begin(),sdi); .br // print it out again .br cout << sd << endl << endl; .br .br // Make another set and an empty result set .br set_type sd2, sdResult; .br for (i = 1; i < 9; i++) .RE .RS 1 sd2.insert(i+5); .RE .RS 0 cout << sd2 << endl; .br // Try a couple of set algorithms .br set_union(sd.begin(),sd.end(),sd2.begin(),sd2.end(), .RE .RS 9 inserter(sdResult,sdResult.begin())); .RE .RS 0 cout << "Union:" << endl << sdResult << endl; .br sdResult.erase(sdResult.begin(),sdResult.end()); .br set_intersection(sd.begin(),sd.end(), .RE .RS 16 sd2.begin(),sd2.end(), .br inserter(sdResult,sdResult.begin())); .RE .RS 0 cout << "Intersection:" << endl << sdResult << endl; .br return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br 0 1 2 3 4 5 6 7 8 9 .br .br 5 6 7 8 9 .br .br 6 7 8 9 10 11 12 13 .br Union: .br 5 6 7 8 9 10 11 12 13 .br Intersection: .br 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 set \f2\fP that takes two templatized iterators: .br template .br set (InputIterator, InputIterator, .br const Compare& = Compare(), .br 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, 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), 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: .br int intarray[10]; .br set first_set(intarray, intarray + 10); .br set second_set(first_set.begin(), .br first_set.end()); but not this way: .br set long_set(first_set.begin(), .br first_set.end()); since the \f2long_set\fP and \f2first_set\fP are 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 need to write: \f2set, allocator >\fP instead of: \f2set\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO allocator, Bidirectional_Iterators, Containers, lexicographical_compare