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
'์๊ณ ๋ฆฌ์ฆ > ์๋ฃ๊ตฌ์กฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] Binary Search Tree (0) | 2024.07.08 |
---|---|
[Java] Tree (0) | 2024.07.07 |
5. ์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ (Circular Linked List) (1) | 2024.02.10 |
4. STL ๋ฐ๋ผ์ก๊ธฐ - ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ (Doubly Linked List) ๊ตฌํ (0) | 2024.02.07 |
3. STL ๋ฐ๋ผ์ก๊ธฐ - ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ (Singly Linked List) ๊ตฌํ (0) | 2024.02.04 |