Compartir a través de


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& 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
   );
set(
   set&& right
);

Parameters

Parameter

Description

Al

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

comp

The comparison function of type const Traits 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 next 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.

The last constructor specifies a copy of the set by moving right.

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, s7_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;

   // Create a set by moving s5
   set<int> s7(move(s5));
   cout << "s7 =";
   for ( s7_Iter = s7.begin( ); s7_Iter != s7.end( ); s7_Iter++ )
      cout << " " << *s7_Iter;
   cout << endl;
}
s1 = 10 20 30 40
s2 = 20 10
s3 = 30
s4 = 10 20 30 40
s5 = 10 20
s6 = 10
s7 = 10 20

The following example demonstrates how to use custom comparators.

#include <iostream>
#include <ostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct Person {
   Person(const string& name, const int age)
      : m_name(name), m_age(age) { }

   string m_name;
   int m_age;
};

struct PersonAgeLess {
   bool operator()(const Person& lhs, const Person& rhs) const {
      return lhs.m_age < rhs.m_age;
   }
};

bool PersonNameGreater(const Person& lhs, const Person& rhs) {
   return lhs.m_name > rhs.m_name;
}

template <typename Container> void print(const string& s, const Container& c) {
   cout << s << ":" << endl;

   for (typename Container::const_iterator i = c.begin(); i != c.end(); ++i) {
      cout << i->m_name << " " << i->m_age << endl;
   }

   cout << endl;
}

int main() {
   vector<Person> v;

   v.push_back(Person("Washington", 12));
   v.push_back(Person("Hayes", 8));
   v.push_back(Person("Bush", 5));
   v.push_back(Person("Garfield", 30));
   v.push_back(Person("Clinton", 7));
   v.push_back(Person("Jefferson", 10));

   set<Person, PersonAgeLess> sl;
   for (vector<Person>::const_iterator i = v.begin(); i != v.end(); ++i) {
      sl.insert(*i);
   }

   set<Person, bool (*)(const Person&, const Person&)> sg(PersonNameGreater);

   for (vector<Person>::const_iterator i = v.begin(); i != v.end(); ++i) {
      sg.insert(*i);
   }

   print("Original vector", v);
   print("Sorted by age (ascending)", sl);
   print("Sorted by name (descending)", sg);
}
Original vector:
Washington 12
Hayes 8
Bush 5
Garfield 30
Clinton 7
Jefferson 10

Sorted by age (ascending):
Bush 5
Clinton 7
Hayes 8
Jefferson 10
Washington 12
Garfield 30

Sorted by name (descending):
Washington 12
Jefferson 10
Hayes 8
Garfield 30
Clinton 7
Bush 5

Requirements

Header: <set>

Namespace: std

See Also

Reference

set Class

Standard Template Library

Other Resources

set Members