次の方法で共有


set::set

Constructs a set that is empty or that is a copy of all or part of some other set.

set( );
explicit set(
   const Traits& _Comp
);
set(
   const Traits& _Comp,
   const Allocator& _Al
);
set(
   const set<Key, Traits, Allocator>& _Right
);
template<class InputIterator>
   set(
      InputIterator _First,
      InputIterator _Last
   );
template<class InputIterator>
   set(
      InputIterator _First,
      InputIterator _Last,
      const Traits& _Comp
   );
template<class InputIterator>
   set(
      InputIterator _First,
      InputIterator _Last,
      const Traits& _Comp,
      const Allocator& _Al
   );

Parameters

  • _Al
    The storage allocator class to be used for this set object, which defaults to Allocator.

  • _Comp
    The comparison function of type constTraits used to order the elements in the set, which defaults to Compare.

  • _Right
    The set of which the constructed set is to be a copy.

  • _First
    The position of the first element in the range of elements to be copied.

  • _Last
    The position of the first element beyond the range of elements to be copied.

Remarks

All constructors store a type of allocator object that manages memory storage for the set and that can later be returned by calling get_allocator. The allocator parameter is often omitted in the class declarations and preprocessing macros used to substitute alternative allocators.

All constructors initialize their sets.

All constructors store a function object of type Traits that is used to establish an order among the keys of the set and that can later be returned by calling key_comp.

The first three constructors specify an empty initial set, the second specifying the type of comparison function (_Comp) to be used in establishing the order of the elements and the third explicitly specifying the allocator type (_Al) to be used. The keyword explicit suppresses certain kinds of automatic type conversion.

The fourth constructor specifies a copy of the set _Right.

The last three constructors copy the range [_First, _Last) of a set with increasing explicitness in specifying the type of comparison function of class Traits and Allocator.

Example

// set_set.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int>::iterator s1_Iter, s2_Iter, s3_Iter, s4_Iter, s5_Iter, s6_Iter;

   // Create an empty set s0 of key type integer
   set <int> s0;

   // Create an empty set s1 with the key comparison
   // function of less than, then insert 4 elements
   set <int, less<int> > s1;
   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );
   s1.insert( 40 );

   // Create an empty set s2 with the key comparison
   // function of geater than, then insert 2 elements
   set <int, greater<int> > s2;
   s2.insert(10);
   s2.insert(20);

   // Create a set s3 with the 
   // allocator of set s1
   set <int>::allocator_type s1_Alloc;
   s1_Alloc = s1.get_allocator( );
   set <int> s3( less<int>( ), s1_Alloc );
   s3.insert( 30 );

   // Create a copy, set s4, of set s1
   set <int> s4( s1 );

   // Create a set s5 by copying the range s1[_First, _Last)
   set <int>::const_iterator s1_bcIter, s1_ecIter;
   s1_bcIter = s1.begin( );
   s1_ecIter = s1.begin( );
   s1_ecIter++;
   s1_ecIter++;
   set <int> s5( s1_bcIter, s1_ecIter );

   // Create a set s6 by copying the range s4[_First, _Last)
   // and with the allocator of set s2
   set <int>::allocator_type s2_Alloc;
   s2_Alloc = s2.get_allocator( );
   set <int> s6( s4.begin( ), ++s4.begin( ), less<int>( ), s2_Alloc );

   cout << "s1 =";
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )
      cout << " " << *s1_Iter;
   cout << endl;
   
   cout << "s2 = " << *s2.begin( ) << " " << *++s2.begin( ) << endl;

   cout << "s3 =";
   for ( s3_Iter = s3.begin( ); s3_Iter != s3.end( ); s3_Iter++ )
      cout << " " << *s3_Iter;
   cout << endl;

   cout << "s4 =";
   for ( s4_Iter = s4.begin( ); s4_Iter != s4.end( ); s4_Iter++ )
      cout << " " << *s4_Iter;
   cout << endl;

   cout << "s5 =";
   for ( s5_Iter = s5.begin( ); s5_Iter != s5.end( ); s5_Iter++ )
      cout << " " << *s5_Iter;
   cout << endl;

   cout << "s6 =";
   for ( s6_Iter = s6.begin( ); s6_Iter != s6.end( ); s6_Iter++ )
      cout << " " << *s6_Iter;
   cout << endl;
}

Output

s1 = 10 20 30 40
s2 = 20 10
s3 = 30
s4 = 10 20 30 40
s5 = 10 20
s6 = 10

Requirements

Header: <set>

Namespace: std

See Also

Concepts

set Class

set Members

Standard Template Library