Set constructors & destructors [C++ Reference]
Keywords: stl,set,set_constructors
Date: 2009-10-09T08:15:36-0700

[[Set constructors & destructors]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- cz
- de
- es
- fr
- it
- jp
- nl
- pl
- ro
- ru
- sk
- tr
- tw

## Set constructors & destructors

Syntax:

        #include <set>
        set();
        set( const set& c );
        set( input_iterator start, input_iterator end );
        set( input_iterator start, input_iterator end, const key_compare& cmp );
        explicit set( const key_compare& cmp );
        ~set();

Every set has a default constructor, copy constructor, and destructor.

The default constructor takes no arguments, creates a new instance of that set, and runs in constant time. The default copy constructor runs in linear time and can be used to create a new set that is a copy of the given set c.

The default destructor is called when the set should be destroyed.

For example, the following code creates and displays a set of integers:

      const int max_nums = 10;
      int nums[max_nums] = {3,1,4,1,5,9,2,6,5,8};

      set<int> digits;
      for( int i = 0; i < max_nums; ++i ) digits.insert(nums[i]);

      cout << "Unique digits are: ";
      for( set<int>::const_iterator iter = digits.begin();
           iter != digits.end();
           ++iter ) {
        cout << *iter << ' ';
      }
      cout << '\n';

When run, this code displays:

    Unique digits are: 1 2 3 4 5 6 8 9

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •