English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ set get_allocator() 사용법 및 예제

C++ STL Set(집합)

C ++ 셋 get_allocator()allocators 객체의 복사를 반환하는 함수이며, set 컨테이너를 생성하는 데 도움이 됩니다.

문법

           allocator_type get_allocator() const; 		//C++ 11 이전
allocator_type get_allocator() const noexcept; 	//C++ 11 이후

파라미터

없음

반환 값

셋 컨테이너와 연관된 할당자를 반환합니다.

복잡

불변합니다.

이터레이터 유효성

변화가 없습니다.

데이터 충돌

컨테이너가 접근됩니다.

set의 요소에 동시에 접근하는 것은 안전합니다.

예외 안전

이 함수는 예외를 던지지 않습니다.

예제1

간단한 예제를 보겠습니다:

#include <iostream>
#include <set>
using namespace std;
int main(void) {
   set<double> m;   
   double *p;
   p = m.get_allocator().allocate(3);
   //double 8
   cout << "할당된 크기 = " << sizeof(*p) * 4 << endl;
   return 0;
}

출력:

할당된 크기 = 32

예제2

간단한 예제를 보겠습니다:

#include <iostream>
#include <set>
using namespace std;
int main
{
  set<int> myset;
  int * p;
  unsigned int i;
  // myset의 배분자를 사용하여 포함하는5요소의 배열:
  p = myset.get_allocator().allocate(5);
  // 배열에 값을 할당합니다
  for (i=0; i<5; i++) p[i] = (i+1)*10;
  cout << "할당된 배열은 포함합니다:";
  for (i=0; i<5; i++) cout << ' ' << p[i];
  cout << '
';
  myset.get_allocator().deallocate(p,5);
  return 0;
}

출력:

할당된 배열은 포함합니다: 10 20 30 40 50

예제3

배분자가 교환 가능한지 확인하는 간단한 예제를 보겠습니다:

#include <set>  
#include <iostream>  
using namespace std;
int main()
{
    
    set<int>::allocator_type s1_Alloc;
    set<int>::allocator_type s2_Alloc;
    set<double>::allocator_type s3_Alloc;
    set<int>::allocator_type s4_Alloc;
    //다음 행은 객체를 선언합니다
    //기본 배분자를 사용합니다。  
    set<int> s1;
    set<int>::allocator_type s2;
    set<double>::allocator_type s3;
    s1_Alloc = s1.get_allocator();
    cout << "할당할 수 있는 정수의 개수"
        << endl << "가용 메모리가 바닥나기 전에: "
        << s2.max_size() << "." << endl;
    cout << "\n할당할 수 있는 두 개의 부동형 수"
        << endl << "가용 메모리가 바닥나기 전에: "
        << s3.max_size() << "." << endl;
    //다음 행은 집합 s를 생성합니다4
    //다중 집합 s를 사용하여1의 배분자.
    set<int> s4(less<int>(), s1_Alloc);
    s4_Alloc = s4.get_allocator();
    //두 배분자가 교환 가능하면
    //각 배분된 스토리지 공간은
    //다른 것이 해제됨
    if (s1_Alloc == s4_Alloc)
    {
        cout << "\n이 배분자는 교환 가능합니다." << endl;
    }
    else
    {
        cout << "\n이 배분자는 교환 불가능합니다." << endl;
    }
   return 0;
}

출력:

할당할 수 있는 정수의 개수
가용 메모리가 바닥나기 전에: 1073741823.
할당할 수 있는 두 개의 부동형 수
가용 메모리가 바닥나기 전에: 536870911.
이 배분자는 교환 가능합니다。

예제4

간단한 예제를 보겠습니다:

#include <iostream>
 #include <set>
using namespace std;
int  main ()} 
{ 
  set < int >  c ; 
  int *  p ;
  p  =  c . get_allocator () . allocate ( 2 );
  p [ 0 ]  =  42 ; 
  p [ 1 ]  =  43 ;
  cout  <<  p [ 0 ]  <<  ", "  <<  p [ 1 ]  <<  endl ;
  c . get_allocator () . deallocate ( p ,  2 ); 
}

출력:

42, 43

C++ STL Set(집합)