Home c++ 표준 템플릿 라이브러리 컨테이너 소개
Post
Cancel

c++ 표준 템플릿 라이브러리 컨테이너 소개

C++ 표준 템플릿 라이브러리 컨테이너 소개

#include <.vector> #include <.deque> #include <.set> #include <.map> #include <.string> #include <.stack> #include <.queue>

sequence_containers [ vector, deque ]


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
void sequence_containers()
{
    // #include <vector>
    {
        vector<int> v;
        for(int i=0; i < 10; i++)
            v.push_back(i);
        
        for(auto &el : v)
            cout << el << " ";
        cout << endl;
        // 0 1 2 3 4 5 6 7 8 9
    }

    // #include <deque>
    {
        deque<int> dq;
        for(int i=0; i < 10; i++)
        {
            dq.push_back(i);
            dq.push_front(i);
        }

        for(auto &el : dq)
            cout << el << " ";
        cout << endl;
        // 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9
    }
}

associative_containers [ set, map ]


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
void associative_containers()
{
    // #include <set> 집합
    {
        set<string> str_set;

        str_set.insert("Hello");
        str_set.insert("World");
        str_set.insert("Hello");

        cout << str_set.size() << endl;

        for(auto &el : str_set)
            cout << el << " ";
        cout << endl;
        // 2
        // Hello World 같은 원소는 추가하지 아니함.
    }

    // multiset : duplication is allowed
    {
        std::multiset<string> str_set;

        str_set.insert("Hello");
        str_set.insert("World");
        str_set.insert("Hello");

        cout << str_set.size() << endl;

        for(auto &el : str_set)
            cout << el << " ";
        cout << endl;
        // 3
        // Hello Hello World 같은 원소를 허락하고 묶어주는 듯 함.

    }

    // #inculde <map>   
    {
        // key / value 가 존재함.
        // json의 구조와 같다곤 함
        // 알아서 정렬된 구조로 나옴   
        std::map<char, int> map;
        map['a'] = 10;
        map['b'] = 20;
        map['c'] = 50;

        cout << map['a'] << endl;
        map['a'] = 100;
        cout << map['a'] << endl;

        for(auto &el : map)
            cout << el.first << " " << el.second << " ";
        cout << endl;
        /*
        10
        100
        a 100 b 20 c 50 
        */
    }

    // multimap : duplicated keys
    // 여러개의 키 값을 갖는다는 뜻 같음.    
    {
        std::multimap<char, int> map;
        map.insert(std::pair('a', 10));
        map.insert(std::pair('b', 10));
        map.insert(std::pair('c', 10));
        map.insert(std::pair('a', 100));
        
        cout << map.count('a') << endl;

        for(auto &el : map)
            cout << el.first << " " << el.second << " ";
        cout << endl;

        // 2 
        // a 10 a 100 b 10 c 10
        // 마찬가지로 정렬되어 나옴 
    }
}

container_adapters [ stack, queue ]


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
45
46
47
48
49
50
51
52
void container_adapters()
{
    // #include <stack>
    {
        cout << "Stack" << endl;

        std::stack<int> stack;
        stack.push(1);      // push adds a copy
        stack.emplace(2);   // emplace constructs a new object
        stack.emplace(3);
        cout << stack.top() << endl;
        stack.pop();
        cout << stack.top() << endl;
        // 3
        // 2 
    }

    // #include <queue>
    {
        cout << "queue" << endl;

        std::queue<int> queue;
        queue.push(1);      // push adds a copy
        queue.push(2);   // emplace constructs a new object
        queue.push(3);
        cout << queue.front() << queue.back() << endl;
        queue.pop();
        cout << queue.front() << queue.back() << endl;
        // 1 3
        // 2 3
    }

    // Priority queue
    {
        // 우선순위 큐 인듯 함. 
        cout << "Priority queue" << endl;

        std::priority_queue<int> queue;

        for(const int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})
            queue.push(n);
        
        for(int i=0; i < 10; i++)
        {
            cout << queue.top() << " ";
            queue.pop();
        }
        cout << endl;
        // 9 8 7 6 5 4 3 2 1 0
    }
}
This post is licensed under CC BY 4.0 by the author.