C++ set


set을 사용하기 위해서는 <set> 헤더를 인클루드 해야한다.


key라 불리는 원소의 집합. key이므로 중복된 값을 가지지 않는다. 중복된 값이 필요하다면 multiset을 이용하면 된다.


set은 균형 이진트리로 구현되어있다.


insert()함수로 데이터를 추가할 수 있으며, 삽입된 데이터는 자동으로 정렬된다. (기본 오름차순)



 생성자

 

 set s

 빈 컨테이너 셋 s 생성

 set s(pred)

 s는 빈 컨테이너 이며, 정렬 기준은 pred 조건자를 사용

 set s(s2)

 셋 s는 셋 s2의 복사본

 set s(b, e)

 셋 s는 반복자 구간 [b, e)로 초기화된 원소를 가짐

 set s(b, e, pred)

 셋 s는 반복자 구간 [b, e)로 초기화된 원소를 가지며, 정렬 기준은 pred 조건자를 사용


생성자 사용 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <set>
#include <functional>
int main()
{
    std::set<int> first;                           // empty set of ints
 
    int myints[] = { 10,20,30,40,50 };
    std::set<int> second(myints, myints + 5);        // range
 
    std::set<int> third(second);                  // a copy of second
 
    std::set<int> fourth(second.begin(), second.end());  // iterator ctor.
 
    std::set<intstd::greater<int> > fifth = { 1020304050 };
 
    std::cout << "first : ";
    for (auto it = first.begin(); it != first.end(); it++)
        std::cout << *it << ' ';
    std::cout << '\n';
 
    std::cout << "second : ";
    for (auto it = second.begin(); it != second.end(); it++)
        std::cout << *it << ' ';
    std::cout << '\n';
 
    std::cout << "third : ";
    for (auto it = third.begin(); it != third.end(); it++)
        std::cout << *it << ' ';
    std::cout << '\n';
 
    std::cout << "fourth : ";
    for (auto it = fourth.begin(); it != fourth.end(); it++)
        std::cout << *it << ' ';
    std::cout << '\n';
 
    std::cout << "fifth : ";
    for (auto it = fifth.begin(); it != fifth.end(); it++)
        std::cout << *it << ' ';
    std::cout << '\n';
 
    return 0;
}
 
cs


실행 결과



주요 멤버 함수

insert() 

 셋에 데이터 삽입

 erase()

 셋에서 원소 제거

 find()

 셋에서 원소의 위치 검색

 count()

 셋에서 원소의 개수 검색

 clear()

 셋의 모든 원소 제거


사용예제

insert()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <set>
 
int main()
{
    std::set<int> myset;
    std::set<int>::iterator it;
    std::pair<std::set<int>::iteratorbool> ret;
 
    // set some initial values:
    for (int i = 1; i <= 5++i) myset.insert(i * 10);    // set: 10 20 30 40 50
 
    ret = myset.insert(20);               // no new element inserted
 
    if (ret.second == false) it = ret.first;  // "it" now points to element 20
 
    myset.insert(it, 25);                 // max efficiency inserting
    myset.insert(it, 24);                 // max efficiency inserting
    myset.insert(it, 26);                 // no max efficiency inserting
 
    int myints[] = { 5,10,15 };              // 10 already in set, not inserted
    myset.insert(myints, myints + 3);
 
    std::cout << "myset contains:";
    for (it = myset.begin(); it != myset.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
 
    return 0;
}
cs



erase(), find()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <set>
 
int main()
{
    std::set<int> myset;
    std::set<int>::iterator it;
 
    // set some initial values:
    for (int i = 1; i <= 5; i++) myset.insert(i * 10);    // set: 10 20 30 40 50
 
    //20, 40의 위치를 찾은 후 제거
    it = myset.find(20);
    myset.erase(it);
    myset.erase(myset.find(40));
 
    std::cout << "myset contains:";
    for (it = myset.begin(); it != myset.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
 
    return 0;
}
cs


count()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <set>
 
int main()
{
    std::set<int> myset;
 
    // set some initial values:
    for (int i = 1; i<5++i) myset.insert(i * 3);    // set: 3 6 9 12
 
    for (int i = 0; i<10++i)
    {
        std::cout << i;
        if (myset.count(i) != 0)
            std::cout << " is an element of myset.\n";
        else
            std::cout << " is not an element of myset.\n";
    }
 
    return 0;
}
cs



참조: http://www.cplusplus.com/reference/set/set/

참고문헌: 뇌를 자극하는 C++ STL

'컴퓨터공학 > STL' 카테고리의 다른 글

[STL] C++ STL map  (0) 2018.07.22
[STL] C++ STL multiset  (0) 2018.07.22
[STL] C++ STL string to int/float/double/long long  (0) 2018.07.19
[STL] C++ STL list  (0) 2018.07.19
[STL] C++ STL deque  (0) 2018.07.19

+ Recent posts