๋๋์ด ์ปจํ ์ด๋ ์ฑํฐ๋ก ๋์ด๊ฐ๋ค !!
STL Sequence Container์ ๊ฐ์ฒด๋ฅผ sequential oreder (์์ฐจ์ ) ์ผ๋ก ์ ์ฅํ๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ด๋ค
Sequence Containers | Type of Iterator Supported | description |
vector (implemented as a dynamic array) | Random Access | ๋์ ๋ฐฐ์ด๋ก ๊ตฌํ๋ ์๋ฃ๊ตฌ์กฐ, ๋ฐฐ์ด๊ณผ ์ ์ฌํ ์ ํ๊ตฌ์กฐ๋ก ๋ฐ์ดํฐ๋ฅผ ์ฐ์์ ์ผ๋ก ์ ์ฅํ์ฌ ๋์ ์ผ๋ก ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ ์ ์์. ๋ฐ์ดํฐ ์ฝ์ , ์ญ์ ๊ฐ ์ฆ์ ๊ฒฝ์ฐ์๋ ์ ์ ํ์ง ์์. |
list (implemented as a doubly-linked list) | Bidirectional | ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ก ๊ตฌํ๋ ์๋ฃ๊ตฌ์กฐ, ๊ฐ ์์๊ฐ ์ด์ ์์์ ๋ค์ ์์์ ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์๊ณ ๋ฐ์ดํฐ๋ฅผ ์์ฐจ์ ์ผ๋ก ์ ์ฅํจ. ๋ฐ์ดํฐ ์ฝ์ ์ญ์ ๊ฐ ์ฆ์ ๊ฒฝ์ฐ์๋ ๋น ๋ฅธ ์ฑ๋ฅ์ ๋ณด์ด์ง๋ง, ์์์ ์์น์ ์๋ ์์์ ์ ๊ทผํ ๋๋ ํจ์ฉ์ฑ์ด ๋จ์ด์ง. |
* Random Access : ๋ฐฐ์ด์ ์์๋ฅผ ์์์ ์์น์์ ์ง์ ์ก์ธ์ค ํ ์ ์๋ ๊ธฐ๋ฅ
-> ์ธ๋ฑ์ค ์ฐ์ฐ์(sunscript operator) ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ํน์ ์์์ ์ก์ธ์ค : array[i]
vector container
- implemented as a dynamic array : ๋์ ๋ฐฐ์ด๋ก ๊ตฌํ๋จ. array์ ์ ์ฌํ๊ฒ ์ฐ์์ ์ธ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์์๋ฅผ ์ ์ฅํ๊ณ ํ์์ ๋ฐ๋ผ ํฌ๊ธฐ๋ฅผ ๋์ ์ผ๋ก ์กฐ์ ํ ์ ์์.
- can access elements randomly (์์์ ์ผ๋ก ์ ๊ทผ)
- ๊ธฐ๋ณธ์์ฑ์(default constructor)์ด์ธ์๋ ๋ค์ํ ์์ฑ์๋ฅผ ํฌํจ -> ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก vector์ ์ด๊ธฐํํ๊ณ ์์ฑ๊ฐ๋ฅ
- size -> number of elements / capcity -> how much memeory is allocated to contain elements.
๊ธฐ๋ณธ ์์ฑ์ default constructor
.reserve()
vector<int> vector1; //capacity = 0
vector1.reserve(20);
//reserve() sets an initial capacity for the vector = 20
vector1.push_back(10); //capacity = 1
vector1.push_back(20); //capacity = 2
๋ณต์ฌ ์์ฑ์ copy constructor
vector<int> vector2(vector1);
vector<int> vector3 = vector1;
์ด๊ธฐํ ๋ฆฌ์คํธ ์์ฑ์ initializer list constructor
vector<int> vector4{ 130, 750, 259, 1234};
vector<int> vector5 = {234, 54, 5634, 65};
// set size and capacity right away + with specific elements!!
๋ฒ์ ์์ฑ์ Range constructor
vector<int> vector5 {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
vector<int> vector6 (vector5.begin() + 2, vector5.begin() +6);
//vector6 becomes : 30 40 50 60
- (20, 50) "closed range" => 20 30 40 50
- [20, 50] "open range" => 30 40
- [20, 50) "half open range" => 30 40 50
vector capacity
- vector grows automatically; by default their capacity is increased as needed
- tho do not shrink automatically; maintain the same capacity
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec;
std::cout << "Initial capacity: " << vec.capacity() << std::endl;
for (int i = 0; i < 10; ++i)
{
vec.push_back(i); //vector capacity automatically grows
std::cout << "Size: " << vec.size() << ", Capacity: " << vec.capacity() << std::endl;
}
return 0;
}
elements๋ฅผ ์ ์ฅํ ๊ณต๊ฐ์ด ์์๊ฒฝ์ฐ... -> ๋์ ๋ฐฐ์ด์ด ์์ฑ (dynamic array) -> ๋ชจ๋ elements๊ฐ ์๋ก์ด ๋ฐฐ์ด์ ๋ณต์ฌ๋จ
Inserting Elements
iterator insert(const_iterator position, cosnt value_type& val);
//๋ฃ๊ณ ์ถ์ position, ๋ฃ๊ณ ์ถ์ single element
iterator insert(const_iterator position, size_type n, const value_type& val);
//๋ฃ๊ณ ์ถ์ position, ๋ฃ๊ณ ์ถ์ ํ์, ๋ฃ๊ณ ์ถ์ single element
iterator insert(const_iterator position, initializer_list<value_type> il);
//๋ฃ๊ณ ์ถ์ position, ๋ฃ๊ณ ์ถ์ initializer list object
vector<int> vector1 = {10, 20, 30, 40};
vector1.insert(vector1.begin() + 2, 100); // 10 20 100 30 40
vector1.insert(vector1.begin() + 2, 3, 100); // 10 20 100 100 100 30 40
vector1.insert(vector1.begin() + 2, {1, 2, 3, 4}); // 10 20 1 2 3 4 30 40
Subscript Operator ์ฒจ์ ์ฐ์ฐ์
vector<int> vector1 = {1, 2, 3, 4, 5};
vector1[2] = 999;
//vector1 is 1 2 999 4 5
Other STL vector functions
front() / back() / begin() / end() / erase() / pop_back()
vector<string> names = { "Ted", "Sue", "Bob", "Jill"};
// Return the first element.(/= .begin())
cout << names.front() << "\n"; //Ted
// Return the last element.(/= .end())
cout << names.back() << "\n"; //Jill
// Delete "Bob".
names.erase(names.begin() + 2); // vector is: Ted Sue Jill
// Delete the last element.(which is Jill)
names.pop_back(); // vector is: Ted Sue
list container
- implemented as a doubly-linked list : ์ด์ค์ฐ๊ฒฐ๋ฆฌ์คํธ๋ก ๊ตฌํ๋จ. ์ด๋ ์ฐ์์ ์ธ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์์๋ฅผ ์ ์ฅํ์ง์๋๋ค๋ ๋ง๊ณผ ๊ฐ์.. -> ๋ฆฌ์คํธ์ ๊ฐ์์๋ ๋ค์ ์์๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ๋ฅผ ํฌํจํ๊ณ ์์ผ๋ฏ๋ก, ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ ๊ฑฐํ ๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌํ ๋น ํ ํ์๊ฐ ์์. ๋ฐ๋ผ์ ๋ฆฌ์คํธ๋ ๋์ ์ผ๋ก ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๋๋ฐ์ ํจ์จ์ ์ด๋ค.
- has no random access (์์์ ์ผ๋ก ์ ๊ทผ ๋ถ๊ฐ๋ฅ!!!!) -> ++iter, --iter ์ ๊ฐ๋ฅํ์ง๋ง (iter +2) ๋ ๋ถ๊ฐ๋ฅ!!!!
- ์ฒจ์ ์ฐ์ฐ์ ์ฌ์ฉ๋ถ๊ฐ ! (subscript operator)
- slist(singly-linked-list) ๋ ์กด์ฌํจ ใ
๊ธฐ๋ณธ ์์ฑ์ default constructor
* By default, an empty list will be created of size 0.
list<int> list1;
list1.push_back(10);
list1.push_back(80);
list1.push_back(54); // list is now: 10 80 54
list1.push_front(3);
list1.push_front(9); // list is now: 9 3 54 80 10
๋ณต์ฌ ์์ฑ์ copy constructor
list<int> list2(list1); //list1์ด ๊ทธ๋๋ก ๋ณต์ฌ๋ list2์์ฑ
list<int> list3 = list1;
์ด๊ธฐํ ๋ฆฌ์คํธ ์์ฑ์ initializer list constructor
list<int> list4{ 130, 750, 259, 1234};
list<int> list5 = {234, 54, 5634, 65};
// set size and capacity right away + with specific elements!!
๋ฒ์ ์์ฑ์ Range constructor
list<int> list5 {10, 20, 30, 40};
list<int>::iterator iter = list5.begin();
++iter;
++iter;
list<int> list6(iter, list5.end());
//list6 contains : 30 40
//list cannot use list5.begin() + 2!!!! ์์์ ์ ๊ทผ ๋ถ๊ฐ๋ฅ!
For loop to Increment
list<int> aList = { 1, 2, 3, 4, 5, 6, 7, 8 };
auto iter = aList.begin(); // iter์ 1
for (int i = 0; i < 6; ++i) // ์ด 6 ๋ฒ ๋ฐ๋ณต
++iter; // 1์์ ์์ํด์ 2 3 4 5 6 7 (6๋ฒ๋ฐ๋ณต)
cout << *iter; // will print 7
Transferring elements
list::splice()
void splice(const_iterator position, list& x);
void splice(const_iterator position, list& x, const_iterator first, const_iterator last);
list<int> nums1 = { 5, 4, 3, 2, 1};
list<int> nums2 = { 77, 66, 55};
nums1.splice(++nums1.begin(), nums2); //nums1 is : 5 77 66 55 4 3 2 1 , num2 is empty
nums1.splice(nums1.begin(), ++nums2.begin(), nums2.end()); //nums1 is : 66 55 5 4 3 2 1 , nums2 is 77
Merging lists
void sort();
void merge(list& x);
list<int> nums5 = { 1, 7, 3, 4, 5};
list<int> nums6 = { 999, 888, 777};
list<int> nums7 = { 89, 23, 874};
nums5.sort(); // nums5 is : 1 3 4 5 7
nums6.sort(); // nums6 is : 777 888 999
nums5.merge(nums6) // nums5 is : 1 3 4 5 7 777 888 999, num6 is empty
//what happens if we do not sort the list?
nums7.merge(nums6); // nums7 is : ...์กธ๋ผ ๋ณต์กํด์ง. ๊ทธ๋ฅ sortํ์ ใ
Other STL list functions
push_front() / pop_front() / reverse() / sort() / remove()
list<int> scores = { 24, 76, 99, 87 };
// Add a new element to the front.
scores.push_front(75); // list is: 75 24 76 99 87
// Delete the front element.
scores.pop_front(); // list is: 24 76 99 87
// Reverse the order of elements.
scores.reverse(); // list is: 87 99 76 24
// Sort the list.
scores.sort(); // list is: 24 76 87 99
// Delete a given element in the list.
scores.remove(87); // list is: 24 76 99
Returning Iterators
std::vector::erase
iterator erase(const_iterator position);
vector<int> v = {1, 4, 5, 2, 3};
v.erase(v.begin() + 3;); // v is : 1 4 5 3
vector<int> v = {1, 4, 5, 2, 3};
vector<int>::iterator iter = v.erase(v.begin() + 3);
/* ๋จผ์ v.begin() + 3์ ํตํด ์ธ๋ฑ์ค๊ฐ 3์ธ ์์์ ๋ํ ๋ฐ๋ณต์๋ฅผ ์ป๊ณ ,
v.erase() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ์์๋ฅผ ์ญ์ ํฉ๋๋ค.
์ด ๋ฐ๋ณต์๋ ์ญ์ ๋ ์์์ ์์น๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋ฉ๋๋ค. */
v.insert(iter, 100); // v is : 1 4 5 100 3
Q. iter์ด ์ธ๋ฑ์ค๊ฐ 3์ธ ์์์ ๋ํ ๋ฐ๋ณต์๋ฅผ ์ป๊ณ , ๊ทธ๊ฑธ ์ญ์ ํจ. ๊ทธ๋ผ ์์น๊ฐ(position)์ ๊ธฐ๋กํ๊ณ , ๊ทธ value๋nullptr ์ด ๋๋๊ฑด๊ฐ??
A.
๋ ! ์ด๋ ต๋ค ..
'เซฎโหถแต แต แตหถโแโก > coding' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] STL Pair, Nested Containers ์ค์ฒฉ ์ปจํ ์ด๋ (0) | 2024.03.31 |
---|---|
[C++] STL ์ฐ๊ด์ปจํ ์ด๋ STL Associative Container (sets and multisets, maps and multimaps) (3) | 2024.03.29 |
[C++] ostream iterator ์ถ๋ ฅ์คํธ๋ฆผ ๋ฐ๋ณต์ + etc... (0) | 2024.03.29 |
[C++] Cycling Iterators ์ํ ๋ฐ๋ณต์ (0) | 2024.03.29 |
[C++] iterator's function (0) | 2024.03.29 |