The set associative container (from header <set>
) is used for fast storage and retrieval of unique keys. The implementation of a set
is identical to that of a multiset
, except that a set
must have unique keys. Therefore, if an attempt is made to insert a duplicate key into a set
, the duplicate is ignored; because this is the intended mathematical behavior of a set, we do not identify it as a common programming error. A set
supports bidirectional iterators (but not random-access iterators). If the order of the keys is not important, you can use unordered_set
(header <unordered_set>
) instead. Figure 15.16 demonstrates a set
of double
s.
1 // Fig. 15.16: fig15_16.cpp
2 // Standard Library set class template.
3 #include <iostream>
4 #include <array>
5 #include <set>
6 #include <algorithm>
7 #include <iterator> // ostream_iterator
8 using namespace std;
9
10 int main()
11 {
12 const size_t SIZE = 5;
13 array< double, SIZE > a = { 2.1, 4.2, 9.5, 2.1, 3.7 };
14 set< double, less< double > > doubleSet( a.begin(), a.end() );
15 ostream_iterator< double > output( cout, " " );
16
17 cout << "doubleSet contains: ";
18 copy( doubleSet.begin(), doubleSet.end(), output );
19
20 // insert 13.8 in doubleSet; insert returns pair in which
21 // p.first represents location of 13.8 in doubleSet and
22 // p.second represents whether 13.8 was inserted
23 auto p = doubleSet.insert( 13.8 ); // value not in set
24 cout << "
" << *( p.first )
25 << ( p.second ? " was" : " was not" ) << " inserted";
26 cout << "
doubleSet contains: ";
27 copy( doubleSet.begin(), doubleSet.end(), output );
28
29 // insert 9.5 in doubleSet
30 p = doubleSet.insert( 9.5 ); // value already in set
31 cout << "
" << *( p.first )
32 << ( p.second ? " was" : " was not" ) << " inserted";
33 cout << "
doubleSet contains: ";
34 copy( doubleSet.begin(), doubleSet.end(), output );
35 cout << endl;
36 } // end main
doubleSet contains: 2.1 3.7 4.2 9.5
13.8 was inserted
doubleSet contains: 2.1 3.7 4.2 9.5 13.8
9.5 was not inserted
doubleSet contains: 2.1 3.7 4.2 9.5 13.8
Line 14 creates a set
of double
s ordered in ascending order, using the function object less<double>
. The constructor call takes all the elements in array a
and inserts them into the set
. Line 18 uses algorithm copy
to output the contents of the set
. Notice that the value 2.1
—which appeared twice in array
—appears only once in doubleSet
. This is because container set
does not allow duplicates.
Line 23 defines and initializes a pair
to store the result of a call to set
function insert
. The pair
returned consists of a const_iterator
pointing to the item in the set
inserted and a bool
value indicating whether the item was inserted—true
if the item was not in the set
; false
if it was.
Line 23 uses function insert
to place the value 13.8
in the set
. The returned pair
, p
, contains an iterator p.first
pointing to the value 13.8
in the set
and a bool
value that’s true
because the value was inserted. Line 30 attempts to insert 9.5
, which is already in the set. The output shows that 9.5
was not inserted again because set
s don’t allow duplicate keys. In this case, p.first
in the returned pair
points to the existing 9.5
in the set
.