public member function

std::set::set

<set>
explicit set ( const Compare& comp = Compare(),
               const Allocator& = Allocator() );
template <class InputIterator>
  set ( InputIterator first, InputIterator last,
        const Compare& comp = Compare(), const Allocator& = Allocator() );
set ( const set<Key,Compare,Allocator>& x );
Construct set
Constructs a set container object, initializing its contents depending on the constructor version used:

explicit set ( const Compare& comp = Compare(), Allocator& = Allocator() );
Default constructor: constructs an empty set object, with no content and a size of zero.
template <class InputIterator> set ( InputIterator first, InputIterator last, const Compare& comp= Compare(), const Allocator& = Allocator() );
Iteration constructor: Iterates between first and last, setting a copy of each of the sequence of elements as the content of the container object.
set ( const set<Key,Compare,Allocator>& x );
Copy constructor: The object is initialized to have the same contents and properties as the x set object.

Parameters

first, last
Input iterators to the initial and final positions in a sequence. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last.
The function template type can be any type of input iterator.
x
Another set object with the same class template parameters (Key, Compare and Allocator).
comp
Comparison object to be used for the strict weak ordering.
Compare is the second class template parameter (see class description).
unnamed Allocator parameter
Allocator object to be used instead of constructing a new one.
For class instantiations using their version of the default allocator class template, this parameter is not relevant.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// constructing sets
#include <iostream>
#include <set>
using namespace std;

bool fncomp (int lhs, int rhs) {return lhs<rhs;}

struct classcomp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}
};

int main ()
{
  set<int> first;                           // empty set of ints

  int myints[]= {10,20,30,40,50};
  set<int> second (myints,myints+5);        // pointers used as iterators

  set<int> third (second);                  // a copy of second

  set<int> fourth (second.begin(), second.end());  // iterator ctor.

  set<int,classcomp> fifth;                 // class as Compare

  bool(*fn_pt)(int,int) = fncomp;
  set<int,bool(*)(int,int)> sixth (fn_pt);  // function pointer as Compare

  return 0;
}


The code does not produce any output, but demonstrates some ways in which a set container can be constructed.

Complexity

For the default constructor, constant.
For the iterator constructor, linear in the distance between the iterators (copy constructions) if the elements are already sorted according to comp. For unsorted sequences, linearithmic (N*logN) in that distance (sorting,copy constructions).
For the copy constructor, linear in x's size (copy constructions).

See also