previous | start | next

Iterators

In C++ iterators are modeled after pointers into arrays. That is, pointers to arrays satisfy the properties of iterators and iterators generalize these properties to other data structures such as lists, dictionaries, etc.

    vector<string> v; // empty vector of strings
    vector<bool> w(10, false); // vector with 10 elements with 
                                     // each element set to false
    int a[] = {1,2,3,5,8,13,21,34};  // int array with 8 elements
    vector z(a, a + 8);              // vector initialized with 
                                     // the 8 elements from a.
    vector::iterator startz = z.begin();
    vector::iterator limitz = z.end();
    vector::iterator i;
   
    bool found = false;
    int x;
    cin >> x;
    for(i = startz; i != limitz; ++i) {
      if ( *i == x ) {
         found = true;
         break;
      }
    }



previous | start | next