다음을 통해 공유


set::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
);

매개 변수

Parameter

설명

Al

기본적으로이 집합 개체에 대해 사용할 저장소 할당자 클래스 할당자.

comp

비교 함수 형식의 const특성 요소 집합에서 기본으로 사용 되는 Compare.

right

집합의 복사본으로 구성 된 집합입니다.

first

복사할 요소의 범위에 있는 첫 번째 요소의 위치입니다.

last

복사할 요소의 범위를 벗어나는 첫 번째 요소의 위치입니다.

설명

모든 생성자는 메모리 저장소 집합을 관리 하 고 나중에 반환할 호출 하 여 할당자 객체 유형을 저장할 get_allocator.할당자 매개 변수 클래스 선언과 전처리 매크로 대체 할당자를 대체 하는 데 종종 생략 합니다.

모든 생성자의 집합을 초기화합니다.

모든 생성자 함수 개체의 형식 저장 특성 사용 되는 키 집합 간의 순서를 설정 하 고 나중에 반환할 호출 하 여 key_comp.

빈 초기 설정, 두 번째는 처음 세 명의 생성자를 지정 비교 함수의 형식을 지정 (comp) 입력 요소와 세 번째 순서를 설정 하는 데 사용할 할당자를 명시적으로 지정 (al) 데.키워드 명시적 자동 형식 변환을 특정 종류를 표시 하지 않습니다.

네 번째 생성자 집합 사본을 지정 right.

범위는 다음 세 명의 생성자 복사 [first, last)의 증가 비교 함수 클래스의 형식을 지정 하는 명시적인 성분할당자.

이동 하 여 마지막으로 생성자 집합 사본을 지정 right.

예제

// 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;
}
  

다음 예제에서는 사용자 지정 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);
}
  

요구 사항

헤더: <set>

네임 스페이스: std

참고 항목

참조

set Class

표준 템플릿 라이브러리