Both sides previous revisionPrevious revisionNext revision | Previous revision |
cpp:stl:iterators [2023/05/29 17:54] – [Κοινές συναρτήσεις για όλους τους iterators] gthanos | cpp:stl:iterators [2023/05/30 18:48] (current) – gthanos |
---|
| |
* **Input Iterator:** Πρόκειται για έναν //iterator// από τον οποίο μπορούμε μόνο να διαβάσουμε το τρέχον στοιχείο της δομής στο οποίο δείχνει. Πρόκειται για έναν //iterator// ανάγνωσης του περιεχόμενου της δομής. Όλοι οι διαθέσιμοι //iterators// είναι //Input Iterators//. Παράδειγμα: | * **Input Iterator:** Πρόκειται για έναν //iterator// από τον οποίο μπορούμε μόνο να διαβάσουμε το τρέχον στοιχείο της δομής στο οποίο δείχνει. Πρόκειται για έναν //iterator// ανάγνωσης του περιεχόμενου της δομής. Όλοι οι διαθέσιμοι //iterators// είναι //Input Iterators//. Παράδειγμα: |
<code cpp> | |
| <code cpp input_iterator.cpp> |
| #include <vector> |
| #include <iostream> |
| #define SIZE 10 |
| using namespace std; |
| |
| int main() { |
| vector<int> ints; |
| for(int i=0; i<10; i++) |
| ints.push_back(SIZE-i); |
| |
for(vector<int>::const_iterator it=ints.cbegin(); it!=ints.cend(); it++) | for(vector<int>::const_iterator it=ints.cbegin(); it!=ints.cend(); it++) |
cout << *it << " "; | cout << *it << " "; |
cout << endl; | cout << endl; |
| } |
</code> | </code> |
* **Output Iterator:** Πρόκειται για έναν //iterator// μέσω του οποίου μπορούμε να διαβάσουμε και να γράψουμε στο τρέχον στοιχείο του //container//. Οι //iterators// που στον τύπο τους περιέχουν το συνθετικό //const_// (const_iterator, const_reverse_iterator) δεν ανήκουν στη συγκεκριμένη κατηγορία. Παραδείγματα: | * **Output Iterator:** Πρόκειται για έναν //iterator// μέσω του οποίου μπορούμε να διαβάσουμε και να γράψουμε στο τρέχον στοιχείο του //container//. Οι //iterators// που στον τύπο τους περιέχουν το συνθετικό //const_// (const_iterator, const_reverse_iterator) δεν ανήκουν στη συγκεκριμένη κατηγορία. Παραδείγματα: |
<code cpp> | <code cpp output_iterator.cpp> |
| #include <vector> |
| #include <iostream> |
| #define SIZE 10 |
| using namespace std; |
| |
| int main() { |
| vector<int> ints; |
| for(int i=0; i<10; i++) |
| ints.push_back(SIZE-i); |
| |
for(vector<int>::iterator it=ints.begin(); it!=ints.end(); it++) { | for(vector<int>::iterator it=ints.begin(); it!=ints.end(); it++) { |
*it += 10; // we modify container here | *it += 10; // we modify container here |
cout << *it << " "; | cout << *it << " "; |
} | } |
| cout << endl; |
| |
for(vector<int>::const_iterator it=ints.cbegin(); it!=ints.cend(); it++) { | for(vector<int>::const_iterator it=ints.cbegin(); it!=ints.cend(); it++) { |
cout << *it << " "; | cout << *it << " "; |
} | } |
| } |
</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// (απλά συνδεδεμένη λίστα). Παραδείγματα: |
std::list<int> mylist; | std::list<int> mylist; |
for (int i=0; i<10; i++) mylist.push_back (i*10); | for (int i=0; i<10; i++) mylist.push_back (i*10); |
| |
| for(std::list<int>::const_iterator it = mylist.cbegin(); it!=mylist.cend(); ++it) |
| std::cout << *it << " "; |
| std::cout << std::endl; |
| |
std::list<int>::iterator it = mylist.begin(); | std::list<int>::iterator it = mylist.begin(); |
| |
std::cout << "Distance between it1 and it2 is: " << distance(it1, it2) << std::endl; | std::cout << "Distance between it1 and it2 is: " << distance(it1, it2) << std::endl; |
std::cout << "Distance between it2 and it1 is: " << distance(it2, it1) << std::endl; | |
| |
return 0; | return 0; |