| Both sides previous revision
Previous revision
Next revision
|
Previous revision
Next revision
Both sides next revision
|
cpp:stl:iterators [2022/05/26 18:44] gthanos |
cpp:stl:iterators [2022/05/26 18:54] gthanos [STL Iterators] |
| </code> | </code> |
| * **Bidirectional Iterator:** Πρόκειται για iterators που εκτός των πράξεων ''++it'', ''it++'' υποστηρίζουν και τις πράξεις ''%%--%%it'', ''it%%--%%''. //Bidirectional Iterators// είναι όλοι οι //iterators// με εξαίρεση εκείνούς που ανήκουν σε //Unordered Associative Containers// και οι //iterators// του //container forward_list// (απλά συνδεδεμένη λίστα). Παραδείγματα: | * **Bidirectional Iterator:** Πρόκειται για iterators που εκτός των πράξεων ''++it'', ''it++'' υποστηρίζουν και τις πράξεις ''%%--%%it'', ''it%%--%%''. //Bidirectional Iterators// είναι όλοι οι //iterators// με εξαίρεση εκείνούς που ανήκουν σε //Unordered Associative Containers// και οι //iterators// του //container forward_list// (απλά συνδεδεμένη λίστα). Παραδείγματα: |
| <code bidirectional.cpp> | <code cpp bidirectional_iterator.cpp> |
| #include <iostream> | #include <iostream> |
| #include <vector> | #include <vector> |
| #include <iostream> | #include <iostream> |
| #include <vector> | #include <vector> |
| | #define SIZE 10 |
| |
| int main () | int main () { |
| { | std::vector<int> myvector (SIZE); // SIZE default-constructed ints |
| std::vector<int> myvector (5); // 5 default-constructed ints | |
| int i=0; | int i=0; |
| | |
| std::vector<int>::reverse_iterator rit = myvector.rbegin(); | for (std::vector<int>::reverse_iterator rit = myvector.rbegin(); rit!= myvector.rend(); ++rit) |
| for (; rit!= myvector.rend(); ++rit) | |
| *rit = ++i; | *rit = ++i; |
| |
| </code> | </code> |
| * **Random Access Iterators:** Πρόκειται για //iterators// που υποστηρίζουν την πράξη της πρόσθεσης και τις αφαίρεσης μεταξύ του //iterator// και ενός ακεραίου αριθμού (η πράξη είναι ανάλογη της αριθμητικής δεικτών). Οι //iterators// αυτής της κατηγορίας υποστηρίζονται από τους //Sequence Containers// //array//, //vector// και //deque// αλλά όχι από τους //containers list// και //forward_list//. Παράδειγμα: | * **Random Access Iterators:** Πρόκειται για //iterators// που υποστηρίζουν την πράξη της πρόσθεσης και τις αφαίρεσης μεταξύ του //iterator// και ενός ακεραίου αριθμού (η πράξη είναι ανάλογη της αριθμητικής δεικτών). Οι //iterators// αυτής της κατηγορίας υποστηρίζονται από τους //Sequence Containers// //array//, //vector// και //deque// αλλά όχι από τους //containers list// και //forward_list//. Παράδειγμα: |
| <code cpp> | <code cpp random_access_iterator.cpp> |
| | #include <iostream> |
| | #include <vector> |
| | #define SIZE 10 |
| | |
| | using namespace std; |
| | |
| | int main() { |
| | vector<int> ints(SIZE); |
| | for(int i=0; i<SIZE; i++) |
| | ints[i] = i+1; |
| | |
| vector<int>::iterator it = ints.cend(); // starting from the end of the container | vector<int>::const_iterator it = ints.cend(); // starting past the end of the container |
| while(true) { | while(it > ints.cbegin()) { |
| it -= 2; // move back by 2 elements | it -= 2; // move back by 2 elements |
| cout << *it1 << " "; | cout << *it << " "; |
| if(it1 <= ints.cbegin()) // stop when we reach or we have passed the first element | if(it <= ints.cbegin()) // stop when we reach or we have passed the first element |
| break; | break; |
| } | } |
| cout << endl; | cout << endl; |
| | } |
| </code> | </code> |
| |