~ Container๋ฅผ ์ดํด๋ณด๊ธฐ ์ ์ Iterator๋ฅผ ์ดํด๋ด ์๋ค ~
Container classes make an intensive use of iterators to ....
1. ์ปจํ ์ด๋์ ๋ค์ด๊ฐ๋ ๋ฐ์ดํฐ๋ค์ ์ํ์ ๋์
2. ๊ฐ๊ธฐ ๋ค๋ฅธ ์ปจํ ์ด๋๋ค์๊ฒ ๋์ผํ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณต (provide uniform interface)
3. ๋ฐ๋ณต์๋ ํฌ์ธํฐ์ "์ผ๋ฐํ ๋ ๊ฐ๋ "์ด๋ค. -> ํฌ์ธํฐ๋ ์๋! ์ถ์ํ๋ ๊ฐ๋ ! (A generalization of a pointer)
4. An iterator variable points to one data entry in the container
5. Each container has its "own" iterator type -> ๋ฐ์ดํฐํ์ ๋ค์ด ๊ทธ๋ค๋ง์ ๋ฐ๋ณต์ ํ์ ์ ๊ฐ๊ณ ์๋๊ฑฐ๋ ์ ์ฌํจ!
์์ ;
// Container type's own iterator type
std::vector<int> myVector;
std::vector<int>::iterator iter; // ๋ฒกํฐ์ ๋ํ iterator ํ์
std::list<double> myList;
std::list<double>::iterator iter; // ๋ฆฌ์คํธ์ ๋ํ iterator ํ์
std::map<std::string, int> myMap;
std::map<std::string, int>::iterator iter; // ๋งต์ ๋ํ iterator ํ์
std::set<float> mySet;
std::set<float>::iterator iter; // ์ธํธ์ ๋ํ iterator ํ์
// Data type's their own pointer type
int* dynamicArray = new int[10];
int* iterator = dynamicArray; // ๋์ ๋ฐฐ์ด์ ๋ํ ๋ฐ๋ณต์ ํ์
std::string str = "Hello";
std::string::iterator iter = str.begin(); // ๋ฌธ์์ด์ ๋ํ ๋ฐ๋ณต์ ํ์
int arr[5] = {1, 2, 3, 4, 5};
int* iter = arr; // ๋ฐฐ์ด์ ๋ํ ๋ฐ๋ณต์ ํ์
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int* iter = *matrix; // ๋งคํธ๋ฆญ์ค์ ๋ํ ๋ฐ๋ณต์ ํ์
<๋ฐ๋ณต์์ ๋ํ ๋ฉค๋ฒํจ์๋ค - member functions for iterators>
ct.begin() : Returns an iterator for the container ct that points to the first data item in ct(container)
ct.end() : It is a flag and does NOT return the last element - IT IS LIKE NULL !!!!!!
<Iterator Operators>
++iter --iter |
์ ์ ์ฐ์ฐ์ Moves the iterator one position forward/backward |
iter++ iter-- |
ํ์ ์ฐ์ฐ์ Moves the iterator one positionforward/backward |
iter += i iter -= i |
i๋งํผ ํฌ์ง์ ์ ๋ํด ์ฆ๊ฐ or ๊ฐ์ |
*iter | ๋ฐ๋ณต์ ์ฐธ์กฐ ? Returns the value of the item the iterator is pointing to. |
iter1 = iter2 | Assigns one iterator to another The POSITION is assigned (๋ฐ๋ณต์ํฌ์ธํฐ - ์ถ์์ ๊ฐ๋ ์!!!! ๊ฐ ๊ฐ๋ฅดํค๋ value๊ฐ ์๋ ์์น๊ฐ assigned๋จ) |
iter1 == iter2 / iter1 != iter2 | value ๊ฐ ์๋๋ผ position์ผ๋ก ๋น๊ต ! (Will return TRUE/FALSE if the iterators are pointing the same item) |
iter[i] / *(iter + i) | Returns the VALUE of the item that is positioned i indices to the right of where the iterator is positioned. (Does NOT move the iterator) ๋์ ํ๋ฉด์ ์ผ๋ก๋ ๊ฐ์ ์ญํ ์ ํ์ง๋ง, ๋ด๋ถ์ ์ผ๋ก๋ ๋ค๋ฅด๊ฒ ๋์๊ฐ! 1. iter[i] : iter์์ i ๋งํผ ๋จ์ด์ ธ์๋ ๊ณณ์ ์ฐพ๊ณ , ๊ทธ๋ค์์ ๊ทธ ์์น์ ์๋ ๊ฐ์ ์ค 2. *(iter + i) : iter์ i ๋งํผ ์ฆ๊ฐ์ํค๊ณ , ๊ทธ๋ค์์ ๊ทธ ์์น์ ์๋ ๊ฐ์ ์ค |
'เซฎโหถแต แต แตหถโแโก > coding' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] Cycling Iterators ์ํ ๋ฐ๋ณต์ (0) | 2024.03.29 |
---|---|
[C++] iterator's function (0) | 2024.03.29 |
[C++] STL์ด๋? ( STL์ ์ ์์ ๊ตฌ์ฑ์์) (2) | 2024.03.27 |
[C++] ์ด๋ก ๊ณต๋ถ ์ค๋ต๋ ธํธ 1 (0) | 2023.09.22 |
[C++] Introduction to C++ Syntax & Variables (0) | 2023.09.22 |