๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/์ž๋ฃŒ๊ตฌ์กฐ

6. STL ์‚ฌ์šฉํ•˜๊ธฐ - std::list

by ๐Ÿณ Laboon 2024. 2. 11.

STL ์ด๋ž€?

C++ ์—์„œ ์ œ๊ณตํ•˜๋Š” STandard Library๋ฅผ STL์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ž๋ฐ”์—์„œ๋Š” ์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๋Š” API๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ง€๊ธˆ๊นŒ์ง€ list์— ๋Œ€ํ•ด ์ง์ ‘ ์„ค๊ณ„ํ•ด๋ณด์•˜๊ณ  list์— ๋Œ€ํ•œ ์„ค๋ช…์ด ์ถฉ๋ถ„ํ–ˆ๋‹ค.

์ด๋ฒˆ์—๋Š” STL์„ ์ง์ ‘ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์–ด๋–ค ๊ธฐ๋Šฅ์ด ์žˆ๊ณ  ์–ธ์ œ, ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•  ์ง€ ์ƒ๊ฐํ•ด๋ณด๋„๋ก ํ•˜์ž.

โ€ป ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” Method๋Š” ์ œ์™ธํ•œ๋‹ค. (ํšจ์œจ์  ์‚ฌ์šฉ)

std::list 

Construct
   
Method Description
(constuctor) ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ์‹œ ์ž…๋ ฅ ๊ฐ’
operator= = ๊ธฐํ˜ธ์— ๋Œ€ํ•œ ์—ฐ์‚ฐ ๋ฐฉ๋ฒ•

 

Iterators

   
Method Description
begin() ๋ฆฌ์ŠคํŠธ Head์— ์†ํ•œ ๋ฐ์ดํ„ฐ ์œ„์น˜
end() ๋ฆฌ์ŠคํŠธ ๋งˆ์ง€๋ง‰ ์œ„์น˜ != Tail, null ๊ฐ’
rbegin() reverse, ๋ฆฌ์ŠคํŠธ Tail์— ์†ํ•œ ๋ฐ์ดํ„ฐ ์œ„์น˜
rend() reverse, ๋ฆฌ์ŠคํŠธ ๋งˆ์ง€๋ง‰ ์œ„์น˜ != Head, null ๊ฐ’

 

Capacity

   
Method Description
empty() ๋ฆฌ์ŠคํŠธ Head์— ์†ํ•œ ๋ฐ์ดํ„ฐ ์œ„์น˜
size() ๋ฆฌ์ŠคํŠธ ๋งˆ์ง€๋ง‰ ์œ„์น˜ != Tail, null ๊ฐ’

 

Element access:

   
Method Description
front() Head์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜
back() Tail์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜

 

Modifiers:

   
Method Description
push_front() Head๋ฅผ ์ถ”๊ฐ€
push_back() Tail์„ ์ถ”๊ฐ€
pop_front() Head๋ฅผ ์ œ๊ฑฐ
pop_back() Tail์„ ์ œ๊ฑฐ
insert() ํŠน์ • ์œ„์น˜์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€
erase() ํŠน์ • ์œ„์น˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œ

 

Operations:

   
Method Description
sort() ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌ
reverse() ๋ฆฌ์ŠคํŠธ๋ฅผ ๋’ค์ง‘์Œ

 


 

Code
#include <iostream>
#include <list>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    // constructor
    list<int> emptyList1;                    // {}
    list<int> l1(3, 10);                    // {3, 3, 3}
    list<int> l2(l1.begin(), l1.end());        // {3, 3, 3}
    list<int> l3(l1);                        // {3, 3, 3}
    int arr[3] = {1, 2, 3};                    
    list<int> l4 (arr, arr + 3);            // {1, 2, 3}

    // operator=
    list<int> emptyList2 = list<int>();        // {}
    list<int> l5 = l4;                        // {1, 2, 3}
    list<int> l6 = {1, 2, 3};                // {1, 2, 3}

    // Iterators
    for (list<int>::iterator it = l6.begin(); it != l6.end(); it++) {
        cout << *it << " ";
    }
    cout << "\n";


    for (list<int>::reverse_iterator it = l6.rbegin(); it != l6.rend(); it++) {
        cout << *it << " ";
    }
    cout << "\n\n";

    // Capacity
    cout << "emptyList1 is empty ? - " << (emptyList1.empty() ? "Yes\n" : "No\n"); 
    cout << "l6 is empty ? - " << (l6.empty() ? "Yes\n" : "No\n"); 
     cout << "size of l6 : " << l6.size() << "\n\n";

     // Element access
     cout << "l6's front : " << l6.front() << '\n';
     cout << "l6's back : " << l6.back() << "\n\n";

     // Modifiers
     cout << "Stack\n";
     l6.push_back(10);                        // {1, 2, 3, 10}                
     l6.push_back(20);                        // {1, 2, 3, 10, 20}
     while (!l6.empty()) {
         cout << l6.back() << " ";
         l6.pop_back();
    }
    cout << "\n";

    cout << "Queue\n";
    l4.push_front(10);                        // {10, 1, 2, 3}
    l4.push_front(20);                        // {20, 10, 1, 2, 3}
    while (!l4.empty()) {
        cout << l4.front() << " ";
        l4.pop_front();
    }
    cout << "\n";

    cout << "Insert\n"; 
     l5.insert(l5.end(), 5);                    // {1, 2, 3, 5}
     l5.insert(l5.begin(), 42);                // {42, 1, 2, 3, 5}
     auto it = l5.begin();                     // {42(it), 1, 2, 3, 5}
    advance(it, 2);                            // {42, 1, 2(it), 3, 5} 
     l5.insert(it, 16);                        // {42, 1, 16, 2(it), 3, 5}
    l5.insert(it, 3, 25);                    // {42, 1, 16, 25, 25, 25, 2(it), 3, 5}
     l5.insert(it, {3, 0});                    // {42, 1, 16, 25, 25, 25, 3, 0, 2(it), 3, 5}
     for (it = l5.begin(); it != l5.end(); it++) {
         cout << *it << " ";
    }
    cout << "\n"; 

    cout << "Erase\n";
    auto it1 = l5.begin();                     // {42(it1), 1, 16, 25, 25, 25, 3, 0, 2, 3, 5}
    auto it2 = l5.begin();                    // {42(it1, it2), 1, 16, 25, 25, 25, 3, 0, 2, 3, 5}
    advance(it2, 4);                        // {42(it1), 1, 16, 25, 25(it2), 25, 3, 0, 2, 3, 5}
    cout << "value of it1 before deletion : " << *it1 << ", ";
    it1 = l5.erase(it1);                    // {1(it1), 16, 25, 25(it2), 25, 3, 0, 2, 3, 5}
    cout << "value of it1 after deletion : " << *it1 << "\n";
    cout << "value of it2 before deletion : " << *it2 << ", ";
    it2 = l5.erase(it2);                    // {1(it1), 16, 25, 25(it2), 3, 0, 2, 3, 5}
    cout << "value of it2 after deletion : " << *it2 << "\n";
    auto it3 = l5.erase(it1, it2);                        // {25(it1, it2), 3, 0, 2, 3, 5}
    cout << "delete from it1 to it2 : ";
    for (it = l5.begin(); it != l5.end(); it++) {
        cout << *it << ' ';
    }
    cout << "\n";
    cout << "it1 = " << *it1 << ", it2 = " << *it2 << ", it3 = " << *it3 <<"\n"; 
    cout << "↑↑↑↑↑↑↑ ์˜ค๋ฅ˜ ๋ฐœ๊ฒฌ, ๋ฐ˜ํ™˜ ๊ฐ’์€ it2 \n\n";

    // Operations
    l5.sort(); 
    cout << "Sorted N log N\n";
    for (it = l5.begin(); it != l5.end(); it++) {
        cout << *it << " ";
    }
    cout << "\n";

    l5.reverse();
    cout << "Reverse\n";
    for (it = l5.begin(); it != l5.end(); it++) {
        cout << *it << " ";
    }
    cout << "\n";

    return 0;
}

 

์‹คํ–‰๊ฒฐ๊ณผ

 

๋ฒ”์œ„ erase์‹œ it1์ด ๋‚จ์•„์žˆ๋Š” ๊ฒƒ์„ ๋ณด์•„ 2๊ฐ€์ง€๋ฅผ ์˜์‹ฌํ•  ์ˆ˜ ์žˆ์Œ.

1. array ๊ตฌ์กฐ list์ผ ์‹œ, iterator๊ฐ€ index๋ฅผ ์ฐธ์กฐํ•˜๋ฏ€๋กœ it1์ด ๋‚จ์•„ ์žˆ์„ ์ˆ˜ ์žˆ์Œ.

2. Node ๊ตฌ์กฐ list์ผ ์‹œ, iterator๋ฅผ deleteํ•˜์ง€ ์•Š์Œ. ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜

C++ STL์˜ ๊ฒฝ์šฐ, ์›ฌ๋งŒํ•˜๋ฉด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‚ฌ์šฉ๋ฒ•์„ ์ •ํ™•ํžˆ ์ˆ™์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

 

์ฐธ๊ณ 

cpp STL reference

 

https://cplusplus.com/reference/list/list/

difference_typea signed integral type, identical to: iterator_traits ::difference_type usually the same as ptrdiff_t

cplusplus.com

 

Repository

https://github.com/jihwankim128/datastructure

 

GitHub - jihwankim128/datastructure

Contribute to jihwankim128/datastructure development by creating an account on GitHub.

github.com

 

list ๋งŒ๋“ค์–ด ๋ณด๊ธฐ

์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ํฌ์ŠคํŠธ

 

4. STL ๋”ฐ๋ผ์žก๊ธฐ - ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ (Doubly Linked List) ๊ตฌํ˜„

์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ž€ ? ์—ฌ๊ธฐ์„œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ  ์—ฌ๊ธฐ์„œ ๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค. ๋˜, ์•ž์„  ํฌ์ŠคํŒ…์—์„œ ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ ๋ฐฉ๋ฒ•์„ ๋งํ–ˆ๋Š”๋ฐ '์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ' ๋Š” ์–‘๋ฐฉ

dev-dot.tistory.com