using set in c++
The first example shows how to insert elements into a set
and to use iterators to print them
Example:
As usual, the include directive
#include <set>
defines all necessary types and operations of sets.
The type of the container is used in several places, so
first a shorter type name gets defined:
typedef set<int> IntSet;
This statement defines type IntSet as a set for elements
of type int. This type uses the default sorting criterion, which sorts the elements by
using operator <. This means the elements are sorted in ascending order. To sort in descending
order or use a completely different sorting criterion, you can pass it as a second template
parameter. For example, the following statement defines a set type that sorts the elements in descending
order
Note that you have to put a space between the two ">"
characters. >>" would be parsed as shift operator, which would result in a syntax error.
typedef set<int, greater<int> > IntSet;
greater<> is a predefined function object. For a sorting criterion that uses only a part of the data of an object
(such as he ID).
All associative containers provide an insert() member
function to nsert a new element:
coll.insert(3);
coll.insert(1);
...
The new element receives the correct position
automatically according to the sorting criterion. You can't use the push_back() or push_front() functions
provided for sequence containers. They make no sense here because you can't specify the
position of the new element.
After all values are inserted in any order, the state of
the container is as shown in Figure below. The elements are sorted into the internal tree structure
of the container so that the value of the left child of an element is always less (with respect to
the actual sorting criterion) and the value of the right child of an element is always greater.
Duplicates are not allowed in a set, so the container contains the value 1 only once.
Figure . A Set that Has Six Elements
To print the elements of the container, you use the same
loop as in the previous list example. An iterator iterates over all elements and prints them:
IntSet::const_iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
cout << *pos << ' ';
}
Again, because the iterator is defined by the container,
it does the right thing, even if the internal structure of the container is more complicated. For
example, if the iterator refers to the third element, operator ++ moves to the
fourth element at the top. After the next call of operator ++ the iterator refers to the fifth element at the bottom (Figure below).
Figure. Iterator pos Iterating over Elements of a Set
The output of the program is as follows:
1 2 3 4 5 6
If you want to use a multiset rather than a set, you need
only change the type of the container (the header file remains the same):
typedef multiset<int> IntSet;
A multiset allows duplicates, so it would contain two
elements that have value 1. Thus, the output of the program would change to the following:
1 1 2 3 4 5 6
See Also:
No comments:
Post a Comment