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 18:48]
gthanos [STL Iterators]
cpp:stl:iterators [2022/05/26 18:54]
gthanos [STL Iterators]
Line 57: Line 57:
 #include <iostream> #include <iostream>
 #include <vector> #include <vector>
 +#define SIZE 10
  
 int main () { int main () {
-  std::vector<int> myvector (5);  // default-constructed ints+  std::vector<int> myvector (SIZE);  // SIZE default-constructed ints
   int i=0;   int i=0;
      
Line 74: 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