profile-img
์ง€๋ฐ์ด์˜ ํ‹ฐ์Šคํ† ๋ฆฌ
images/slide-image

 

 

~ 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' Related Articles +