cpp:stl:iterators

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
cpp:stl:iterators [2022/05/26 17:39]
gthanos
cpp:stl:iterators [2022/05/26 18:54]
gthanos [STL Iterators]
Line 29: Line 29:
 </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 cpp> +<code cpp bidirectional_iterator.cpp> 
-  for(vector<int>::iterator it=ints.cbegin(); it!=ints.cend()it++) { +#include <iostream> 
-    cout << *it << " "+#include <vector> 
-  } +#define SIZE 10 
-  cout << endl; + 
-   +using namespace std; 
-  vector<int>::iterator it = ints.cend(); // starting from the end of the container+ 
 +int main() { 
 +  vector<int> ints(SIZE); 
 +  for(int i=0i<SIZE; i++) 
 +    ints[i] = i+1
 +  
 +  vector<int>::const_iterator it = ints.cend(); // starting from the end of the container
   while(true) {   while(true) {
-    --it1;                                // move to the previous element +    --it;                                // move to the previous element 
-    cout << *it1 << " "; +    cout << *it << " "; 
-    if(it1 == ints.cbegin())              // stop when we reach the first element+    if(it == ints.cbegin())              // stop when we reach the first element
       break;       break;
   }   }
   cout << endl;   cout << endl;
 +}
 </code> </code>
  
Line 50: Line 57:
 #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);  // 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;
  
Line 70: Line 75:
 </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>
  
cpp/stl/iterators.txt · Last modified: 2023/05/30 18:48 by gthanos