set(3C++) - set(3C++)
Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.
NAMEset
- An associative container that supports unique keys. A set supports
bidirectional iterators.
SYNOPSIS
#include <set>
template <class Key, class Compare = less<Key>,
class Allocator = allocator<Key> >
class set;
DESCRIPTION
set<Key,_Compare,_Allocator>_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 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 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 set {
public:
// types
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 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);
void erase (iterator);
size_type erase (const key_type&);
void 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>&);
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>&);
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
explicit
set(const Compare& comp = Compare(),
const Allocator& alloc = Allocator());
Creates a set of zero elements. If the function object comp 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
less (<). The allocator alloc is used for all storage management.
template <class InputIterator>
set(InputIterator first, InputIterator last,
const Compare& comp = Compare()const Allocator& alloc = Allocator());
Creates a set of length last - first, filled with all values obtained by
dereferencing the InputIterators on the range [first, last). If the func‐
tion object comp 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 less (<). Uses the allocator Allocator()
for all storage management.
set(const set<Key, Compare, Allocator>& x);
Copy constructor. Creates a copy of x.
DESTRUCTORS
~set();
Releases any allocated memory for self.
ASSIGNMENT OPERATORS
set<Key, Compare, Allocator>&
operator=(const set<Key, Compare, Allocator>& x);
Returns a reference to self. Self shares an implementation with 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 that points to the first element in self.
const_iteratorbegin() const;
Returns a const_iterator that points to the first element in self.
iteratorend();
Returns an iterator that points to the past-the-end value.
const_iteratorend() const;
Returns a const_iterator that points to the past-the-end value.
reverse_iteratorrbegin();
Returns a reverse_iterator that points to the past-the-end value.
const_reverse_iteratorrbegin() const;
Returns a const_reverse_iterator that points to the past-the-end value.
reverse_iteratorrend();
Returns a reverse_iterator that points to the first element.
const_reverse_iteratorrend() const;
Returns a const_reverse_iterator that points to the first element.
MEMBER FUNCTIONS
void
clear();
Erases all elements from the set.
size_typecount(const key_type& x) const;
Returns the number of elements equal to x. Since a set supports unique
keys, count always returns 1 or 0.
boolempty() const;
Returns true if the size is zero.
pair<iterator, iterator>
equal_range(const key_type& x) const;
Returns pair(lower_bound(x),upper_bound(x)). The equal_range function indi‐
cates the valid range for insertion of x into the set.
size_typeerase(const key_type& x);
Deletes all the elements matching x. Returns the number of elements
erased. Since a set supports unique keys, erase always returns 1 or 0.
voiderase(iterator position);
Deletes the map 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);
Deletes the elements in the range (first, last). Returns an iterator point‐
ing to the element following the last deleted element, or end() if there
were no elements after the deleted range.
iteratorfind(const key_value& x) const;
Returns an iterator that points to the element equal to x. If there is no
such element, the iterator points to the past-the-end value.
pair<iterator, bool>
insert(const value_type& x);
Inserts x into self according to the comparison function object. The tem‐
plate's default comparison function object is less (<). If the insertion
succeeds, it returns a pair composed of the iterator position where the
insertion took place and true. Otherwise, the pair contains the end value
and false.
iteratorinsert(iterator position, const value_type& x);
x 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 position,
then it takes amortized constant time. Otherwise it takes 0 (log N) time.
The return value points to the inserted x.
template <class InputIterator>
voidinsert(InputIterator first, InputIterator last);
Inserts copies of the elements in the range [first, last].
key_comparekey_comp() const;
Returns the comparison function object for the set.
iteratorlower_bound(const key_type& x) const;
Returns an iterator that points to the first element that is greater than
or equal to x. If there is no such element, the iterator points to the
past-the-end value.
size_typemax_size() const;
Returns the size of the largest possible set.
size_typesize() const;
Returns the number of elements.
voidswap(set<Key, Compare, Allocator>& x);
Exchanges self with x.
iteratorupper_bound(const key_type& x) const
Returns an iterator that points to the first element that is greater than
or equal to x. If there is no such element, the iterator points to the
past-the-end value.
value_comparevalue_comp() const;
Returns the set's comparison object. This is identical to the function
key_comp().
NON-MEMBER OPERATORS
template <class Key, class Compare, class Allocator>
bool operator==(const set<Key, Compare, Allocator>& x,
const set<Key, Compare, Allocator>& y);
Returns true if x is the same as y.
template <class Key, class Compare, class Allocator>
bool operator!=(const set<Key, Compare, Allocator>& x,
const set<Key, Compare, Allocator>& y);
Returns !(x==y).
template <class Key, class Compare, class Allocator>
bool operator<(const set <Key, Compare, Allocator>& x,
const set <Key, Compare, Allocator>& y);
Returns true if the elements contained in x are lexicographically less than
the elements contained in y.
template <class Key, class Compare, class Allocator>
bool operator>(const set <Key, Compare, Allocator>& x,
const set <Key, Compare, Allocator>& y);
Returns y < x.
template <class Key, class Compare, class Allocator>
bool operator<=(const set <Key, Compare, Allocator>& x,
const set <Key, Compare, Allocator>& y);
Returns !(y < x).
template <class Key, class Compare, class Allocator>
bool operator>=(const set <Key, Compare, Allocator>& x,
const set <Key, Compare, Allocator>& y);
Returns !(x < y).
SPECIALIZED ALGORITHMS
template <class Key, class Compare, class Allocator>
void swap(set <Key, Compare, Allocator>& a,
set <Key, Compare, Allocator>& b);
Swaps the contents of a and b.
EXAMPLE
//
// setex.cpp
//
#include <set>
#include <iostream>
using namespace std;
typedef set<double, less<double>, allocator<double> >
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 set of doubles
set_type sd;
int i;
for(i = 0; i < 10; ++i) {
// insert values
sd.insert(i);
}
// print out the setcout << sd << endl << endl;
// now let's erase half of the elements in the setint 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 setset_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;
}
Program Output0 1 2 3 4 5 6 7 8 95 6 7 8 96 7 8 9 10 11 12 13Union:5 6 7 8 9 10 11 12 13Intersection: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 set <Key, Compare, Allocator> that takes two templatized itera‐
tors:
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 fea‐
ture, 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 tem‐
plates, you can construct a set in the following two ways:
int intarray[10];
set<int> first_set(intarray, intarray + 10);
set<int> second_set(first_set.begin(),
first_set.end());
but not this way:
set<long> 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 always need to supply the Compare template
argument and the Allocator template argument. For instance, you need to
write:
set<int, less<int>, allocator<int> >
instead of:
set<int>
If your compiler does not support namespaces, then you do not need the
using declaration for std.
SEE ALSO
allocator, Bidirectional_Iterators, Containers, lexicographical_compare
Rogue Wave Software 02 Apr 1998 set(3C++)