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

C++ 기본 튜토리얼

C++ kiểm soát luồng

C++ hàm

C++ mảng & chuỗi

C++ cấu trúc dữ liệu

C++ lớp & đối tượng

C++ con trỏ

C++ thừa kế

C++ STL 教程

C++ 参考手册

C++ set constructor(构造函数) 使用方法及示例

C++ STL Set(집합)

set构造函数有以下五种用途:

  1. 默认构造函数:用于构造具有零个元素的空set容器。

  2. 范围构造函数:用于构造内容范围为[first,last)的容器。

  3. 复制构造函数:用于构造带有现有容器元素副本的集合。

  4. move构造函数:用于使用move语义与其他元素一起构造容器。

  5. 初始化程序列表构造函数:用于构造带有初始化程序列表内容的集合。

语法

기본 생성자

explicit set (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());	//到 C++ 11
explicit set (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
explicit set (const allocator_type& alloc);			//从C ++ 11开始

范围构造器

template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& alloc = allocator_type());		//到 C++ 11
template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());			//从C ++ 11开始

복사 생성자

set (const set& x);						//到 C++ 11
set (const set& x);
set (const set& x, const allocator_type& alloc);			//从C ++ 11开始

移动构造函数

set (set&& x);
set (set&& x, const allocator_type& alloc);			//从C ++ 11开始

이니셜리스트 생성자

set (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());		//从C ++ 11开始

参数

comp:比较函数对象,它接受两个关键参数,如果第一个参数在第二个参数之前,则返回true,否则返回false。默认情况下,它使用less <key_type>谓词。

alloc:一个分配器对象,用于此容器的所有内存分配。

first:반복에 입력할 이터레이터의 첫 번째 위치。

last:반복에 입력할 이터레이터의 마지막 위치。

x:같은 타입의 다른 셋 객체。

il:요소를 복사할 초기화 목록 객체。

반환 값

생성자는 어떤 값을 반환하지 않습니다.

복잡성

공집합 생성자와 move 생성자의 경우 복잡성은 상수입니다.

그 외 모든 경우, 요소가 정렬되어 있다면 이터레이터 간의 거리의 복잡성은 선형입니다.

이터레이터 유효성

move 생성자에서 셋 컨테이너의 요소가 이동될 경우, x와 관련된 모든 포인터, 이터레이터, 참조는 무효가 됩니다.

데이터 갈등

모든 복사된 요소에 접근합니다.

예외 안전성

예외가 발생하면 어떤 영향도 없습니다.

예제1

기본 생성자의 간단한 예제를 보겠습니다:

#include <iostream>
#include <set>
using namespace std;
int main(void) {
   // 기본 생성자
   set<char> s;
  
   int size = s.size(); 
   cout << "셋 s의 크기 = " << size;
   return 0;
}

출력:

셋 s의 크기 = 0

위의 예제에서 s는 공집합입니다. 따라서 크기는 0입니다.

예제2

아래의 반복 생성자의 간단한 예제를 보겠습니다:

#include <iostream>
#include <set>
using namespace std;
int main(void) {
   int evens[] = {2,4,6,8,10};
  
   // 반복 생성자
   set<int> myset (evens, evens+5);  
   cout << "셋 컨테이너 myset의 크기는 : " << myset.size();
   return 0;
}

출력:

셋 컨테이너 myset의 크기는: 5

위의 예제에서 set myset는 evens 요소로 구성되어 있습니다.

예제3

아래의 간단한 복사 생성자 예제를 보겠습니다:

#include <iostream>
#include <set>
using namespace std;
int main(void) {
   //기본 생성자
   std::set<int> s1;
   s1.insert(5);
   s1.insert(10);
   cout << "셋 컨테이너 s1의 크기는 : " << s1.size();
  
   // 복사 생성자
   set<int> s2(s1);
   cout << "\n새로운 셋 컨테이너 s2의 크기는 : " << s2.size();
   return 0;
}

출력:

셋 컨테이너 s1의 크기는 : 2
새로운 셋 컨테이너 s2의 크기는 : 2

위의 예제에서 s2은 s1셋의 복사 복제본.

예제4

아래의 간단한 move 생성자 예제를 보겠습니다:

#include <iostream>
#include <set>
using namespace std;
int main(void) {
   // 기본 생성자
   set<char> s1;
   s1.insert('x');
   s1.insert('y');
   cout << "셋 컨테이너 s1의 크기는 : " << s1.size();
   // Move 생성자
   set<char> s2(move(s1))
   cout << "\n새로운 셋 컨테이너 s2의 크기는 : " << s2.size();
   return 0;
}

출력:

셋 컨테이너 s1의 크기는 : 2
새로운 셋 컨테이너 s2의 크기는 : 2

위의 예제에서 s1의 내용이 s로 이동됩니다2 set.

예제5

아래의 간단한 초기화 목록 생성자 예제를 보겠습니다:

#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
   // 이니셜리스트 생성자
   set<string> fruit {
      "orange", "apple", "mango", "peach", "grape"
   };
   cout << "컨테이너 내 fruit의 크기는 : " << fruit.size();
   return 0;
}

출력:

컨테이너 내 fruit의 크기는 : 5

위의 예제는 문자열 키로 set을 생성하고 initializer_list로 초기화합니다.

C++ STL Set(집합)