๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/C, C++

[ C++ ] C++์˜ ๋‹จ์ง STL ๐Ÿง๐Ÿป‍โ™€๏ธ

by candosh 2024. 1. 13.

DALL·E๊ฐ€ ๋งŒ๋“ค์–ด์ค€ ์ธ๋„ค์ผ ์‚ฌ์ง„ ใ…Žใ…Ž

 

๐Ÿ’๐Ÿป‍โ™€๏ธ What is STL ?

ํ‘œ์ค€ ํ…œํ”Œ๋ฆฟ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(Standard Template Library, STL)๋Š” C++์˜ ์ผ๋ถ€๋กœ, ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฒ”์šฉ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ œ๊ณตํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

 

๐Ÿ“ฆ ์ปจํ…Œ์ด๋„ˆ

1. vector: ๊ฐ€๋ณ€ ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ์ œ๊ณต. ๋น ๋ฅธ ๋ฌด์ž‘์œ„ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋’ค์ชฝ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•˜๋Š” ์—ฐ์‚ฐ์— ํšจ์œจ์ ์ด๋‹ค.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    vec.push_back(6); // ๋ฒกํ„ฐ ๋์— 6 ์ถ”๊ฐ€
    cout << "The vector contains: ";
    for(int n : vec) {
        cout << n << ' ';
    }
    cout << endl;
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: The vector contains: 1 2 3 4 5 6
}

 

2. deque: ์–‘์ชฝ ๋์—์„œ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ๋”๋ธ” ์—”๋””๋“œ ํ์ด๋‹ค.

#include <iostream>
#include <deque>
using namespace std;

int main() {
    deque<int> d = {2, 3, 4};
    d.push_front(1); // ๋งจ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
    d.push_back(5); // ๋งจ ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
    cout << "The deque is: ";
    for(int n : d) {
        cout << n << ' ';
    }
    cout << endl;
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: The deque is: 1 2 3 4 5
}

 

3. list: ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ, ๋ฆฌ์ŠคํŠธ ์–‘์ชฝ ๋ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ค‘๊ฐ„์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•˜๋Š” ์—ฐ์‚ฐ์— ํšจ์œจ์ ์ด๋‹ค.

#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> lst = {2, 4, 6, 8};
    lst.push_front(1); // ๋งจ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
    lst.push_back(9); // ๋งจ ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
    cout << "The list is: ";
    for(int n : lst) {
        cout << n << ' ';
    }
    cout << endl;
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: The list is: 1 2 4 6 8 9
}

 

 

4. map: ํ‚ค-๊ฐ’ ์Œ์„ ์ €์žฅํ•˜๋Š” ๊ท ํ˜• ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ๊ธฐ๋ฐ˜์˜ ์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค.

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<string, int> ageMap;
    ageMap["Alice"] = 30;
    ageMap["Bob"] = 25
;
cout << "Alice's age: " << ageMap["Alice"] << endl;
cout << "Bob's age: " << ageMap["Bob"] << endl;
return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: Alice's age: 30 // Bob's age: 25
}

 

5. set: ํ‚ค๋งŒ์„ ์ €์žฅํ•˜๋Š” ๊ท ํ˜• ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ๊ธฐ๋ฐ˜์˜ ์ปจํ…Œ์ด๋„ˆ๋กœ, ๋ชจ๋“  ํ‚ค๋Š” ์œ ์ผํ•˜๋‹ค.

#include <iostream>
#include <set>
using namespace std;

int main() {
    set<int> myset;
    myset.insert(3);
    myset.insert(1);
    myset.insert(7); // set์€ ์ž๋™์œผ๋กœ ์ •๋ ฌ๋จ

    cout << "Elements in set: ";
    for(int element : myset) {
        cout << element << ' ';
    }
    cout << endl;
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: Elements in set: 1 3 7
}

 

6. unordered_map์™€ unordered_set: ํ•ด์‹œ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„๋œ ์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ๋กœ, ๋น ๋ฅธ ์ ‘๊ทผ ์‹œ๊ฐ„์„ ์ œ๊ณตํ•œ๋‹ค.

#include <iostream>
#include <unordered_map>
#include <unordered_set>
using namespace std;

int main() {
    unordered_map<string, int> umap;
    umap["one"] = 1;
    umap["two"] = 2;
    umap["three"] = 3;

    cout << "unordered_map elements: ";
    for(auto &pair : umap) {
        cout << pair.first << "=>" << pair.second << ' ';
    }
    cout << endl;

    unordered_set<int> uset = {4, 2, 1};
    cout << "unordered_set elements: ";
    for(int element : uset) {
        cout << element << ' ';
    }
    cout << endl;

    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: unordered_map elements: one=>1 two=>2 three=>3 
              //           unordered_set elements: 1 2 4
}

 

7.stack๊ณผ queue: ๊ฐ๊ฐ ํ›„์ž…์„ ์ถœ(LIFO) ๋ฐ ์„ ์ž…์„ ์ถœ(FIFO) ์ •์ฑ…์„ ๊ตฌํ˜„ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ์–ด๋Œ‘ํ„ฐ์ด๋‹ค.

// stack
#include <iostream>
#include <stack>
using namespace std;

int main() {
    stack<int> st;
    st.push(1); // ์š”์†Œ ์ถ”๊ฐ€
    st.push(2);
    st.push(3);

    cout << "Stack elements: ";
    while (!st.empty()) {
        cout << st.top() << ' '; // ๋งจ ์œ„์˜ ์š”์†Œ ํ™•์ธ
        st.pop(); // ๋งจ ์œ„์˜ ์š”์†Œ ์ œ๊ฑฐ
    }
    cout << endl;
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: Stack elements: 3 2 1
}

 

// queue
#include <iostream>
#include <queue>
using namespace std;

int main() {
    queue<int> q;
    q.push(1); // ์š”์†Œ ์ถ”๊ฐ€
    q.push(2);
    q.push(3);

    cout << "Queue elements: ";
    while (!q.empty()) {
        cout << q.front() << ' '; // ๋งจ ์•ž์˜ ์š”์†Œ ํ™•์ธ
        q.pop(); // ๋งจ ์•ž์˜ ์š”์†Œ ์ œ๊ฑฐ
    }
    cout << endl;
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: Queue elements: 1 2 3
}

 

๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜ 

  1. sort: ์ปจํ…Œ์ด๋„ˆ ๋‚ด์˜ ์š”์†Œ๋ฅผ ์ •๋ ฌํ•œ๋‹ค.
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> vec = {4, 1, 3, 5, 2};
    sort(vec.begin(), vec.end()); // ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    for(int v : vec) {
        cout << v << ' ';
    }
    cout << endl;
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 1 2 3 4 5
}

 

2. find์™€ binary_search: ์š”์†Œ๋ฅผ ์ฐพ๋Š”๋‹ค.

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    auto it = find(vec.begin(), vec.end(), 3);
    if (it != vec.end()) {
        cout << "Element found: " << *it << endl;
    } else {
        cout << "Element not found." << endl;
    }
kotlin
Copy code
return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: Element found: 3
}

 

3. accumulate: ์ปจํ…Œ์ด๋„ˆ ๋‚ด์˜ ์š”์†Œ๋“ค์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•œ๋‹ค.

#include <numeric>
#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    int sum = accumulate(vec.begin(), vec.end(), 0);
    cout << "Sum of elements: " << sum << endl;
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: Sum of elements: 15
}

 

4. replace์™€ replace_if: ํŠน์ • ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ๋ฅผ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๊ต์ฒดํ•œ๋‹ค.

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5, 3};
    replace(vec.begin(), vec.end(), 3, 8); // ๋ชจ๋“  3์„ 8๋กœ ๊ต์ฒด
    for(int v : vec) {
        cout << v << ' ';
    }
    cout << endl;
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 1 2 8 4 5 8
}

 

5. count์™€ count_if: ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5, 3};
    int cnt = count(vec.begin(), vec.end(), 3);
    cout << "Number of occurrences of 3: " << cnt << endl;
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: Number of occurrences of 3: 2
}

 

6. next_permutation: ์ฃผ์–ด์ง„ ๋ฒ”์œ„์˜ ์š”์†Œ๋“ค์˜ ๊ฐ€๋Šฅํ•œ ์ˆœ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3};
    cout << "The 3! possible permutations with 3 elements:\n";
    do {
        for(int v : vec) {
            cout << v << ' ';
        }
        cout << endl;
    } while(next_permutation(vec.begin(), vec.end()));
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 1 2 3, 1 3 2, 2 1 3, 2 3 1, 3 1 2, 3 2 1
}

 

7. lower_bound์™€ upper_bound: ์ •๋ ฌ๋œ ๋ฒ”์œ„์—์„œ ํŠน์ • ๊ฐ’์˜ ํ•˜ํ•œ๊ณผ ์ƒํ•œ์„ ์ฐพ๋Š”๋‹ค.

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
    auto lower = lower_bound(vec.begin(), vec.end(), 3);
    auto upper = upper_bound(vec.begin(), vec.end(), 3);
    cout << "Lower bound for 3: " << (lower - vec.begin()) << endl;
    cout << "Upper bound for 3: " << (upper - vec.begin()) << endl;
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: Lower bound for 3: 3, Upper bound for 3: 6
}

 

๐Ÿ”„ ๋ฐ˜๋ณต์ž

  • STL์€ ์ปจํ…Œ์ด๋„ˆ์˜ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ˜๋ณต์ž(iterator)๋ฅผ ์ œ๊ณตํ•œ๋‹ค. begin(), end(), rbegin(), rend() ๋“ฑ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

1. begin(): ์ปจํ…Œ์ด๋„ˆ์˜ ์‹œ์ž‘ ์ง€์ ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    
    // begin() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ์˜ ์‹œ์ž‘ ๋ฐ˜๋ณต์ž๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
    vector<int>::iterator it = vec.begin();
    
    // ์‹œ์ž‘๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ˆœํšŒํ•˜๋ฉด์„œ ์š”์†Œ ์ถœ๋ ฅ
    while (it != vec.end()) {
        cout << *it << ' ';
        ++it;
    }
    
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 1 2 3 4 5
}

 

2. end(): ์ปจํ…Œ์ด๋„ˆ์˜ ๋์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    
    // end() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ์˜ ๋ ๋ฐ˜๋ณต์ž๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
    vector<int>::iterator it = vec.end();
    
    // ์—ญ์ˆœ์œผ๋กœ ๋๋ถ€ํ„ฐ ์‹œ์ž‘๊นŒ์ง€ ์ˆœํšŒํ•˜๋ฉด์„œ ์š”์†Œ ์ถœ๋ ฅ
    while (it != vec.begin()) {
        --it;
        cout << *it << ' ';
    }
    
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 5 4 3 2 1
}

 

 

3. rbegin(): ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ˆœํšŒํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋ฉฐ, ์ปจํ…Œ์ด๋„ˆ์˜ ๋์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์—ญ์ˆœํ™˜ ๋ฐ˜๋ณต์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    
    // rbegin() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ญ์ˆœํ™˜ ๋ฐ˜๋ณต์ž๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
    vector<int>::reverse_iterator it = vec.rbegin();
    
    // ์—ญ์ˆœ์œผ๋กœ ์š”์†Œ ์ถœ๋ ฅ
    while (it != vec.rend()) {
        cout << *it << ' ';
        ++it;
    }
    
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 5 4 3 2 1
}

 

4. rend(): ์—ญ์ˆœํ™˜ ๋ฐ˜๋ณต์ž์˜ ๋์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    
    // rend() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ญ์ˆœํ™˜ ๋ฐ˜๋ณต์ž์˜ ๋์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
    vector<int>::reverse_iterator it = vec.rend();
    
    // ์—ญ์ˆœ์œผ๋กœ ์š”์†Œ ์ถœ๋ ฅ
    while (it != vec.rbegin()) {
        --it;
        cout << *it << ' ';
    }
    
    return 0; // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 1 2 3 4 5
}

 

โ—๏ธ ๊ทธ๋ž˜์„œ ์ด๊ฒƒ๋งŒ ์•Œ์ž๋Š”?

๋‹ค ์ค‘์š”ํ•˜์ง€๋งŒ ๊ทธ๋ž˜๋„ ~~

 

Vector, deque, List

 

- ๋ฐฑ์ค€ ๋‹ค์ด์•„ 4 ํ”ผ์…œ -