From pointers to iterators(cont,) u For vector, previous function works, because vector can be also sequentially addressed But a small problem: vector is nullable while array not, following usage will cause runtime error vector<string> svecr find( &svec[o], &svec[svec sizeof search_value); We add two supplementary functions for safe
From pointers to iterators (cont.) ◼ For vector, previous function works, because vector can be also sequentially addressed ◼ But a small problem: vector is nullable while array not, following usage will cause runtime error vector<string> svec; find(&svec[0], &svec[svec.size()], search_value); ◼ We add two supplementary functions for safe
From pointers to iterators(cont,) template <typename Elem Type> inline Elem Type* begin(const vector< ElemType> &vec return vec. empty (? 0 &vecO] template <typename Elem Type> inline Elem Type* end(const vector< ElemType> &vec) return vec. empty (?0:++(&veclvec size(-1D) Calling find(begin(svec, end(svec), search_value);
From pointers to iterators (cont.) ◼ Calling find(begin(svec), end(svec), search_value); template <typename ElemType> inline ElemType* begin(const vector<ElemType> &vec) { return vec.empty() ? 0 :&vec[0]; } template <typename ElemType>; inline ElemType* end(const vector<ElemType> &vec) { return vec.empty() ? 0 : ++(&vec[vec.size() – 1]); }
From pointers to iterators(cont,) u Request 4 Let the previous findo function also support the list container in sTl Our arithmetic of pointer in previous function wi not work, because the addressing is different Array and vector using consecutive memory block(++,-) List using linked memory units(->next, ->prev) a To solve it, we must add an abstract layer to encapsulate the low-level pointer actions, thus avoiding user's direct manipulating with low-level pointers Iterators- abstract pointers to containers
From pointers to iterators (cont.) ◼ Request 4: ◼ Let the previous find() function also support the list container in STL ◼ Our arithmetic of pointer in previous function will not work, because the addressing is different ◼ Array and vector using consecutive memory block (++, --) ◼ List using linked memory units (->next, ->prev) ◼ To solve it, we must add an abstract layer to encapsulate the low-level pointer actions, thus avoiding user’s direct manipulating with low-level pointers ◼ Iterators – abstract pointers to containers
Iterators u Objects that defined or overloaded the internal operators(++, x which we could use them as ordinary pointers Each container provides us with kinds of iterators as its nested types and also functions to manipulate them
Iterators ◼ Objects that defined or overloaded the internal operators (++, *, ==, !=), which we could use them as ordinary pointers ◼ Each container provides us with kinds of iterators as its nested types, and also functions to manipulate them
Iterators(cont.) Definition vector<string> svec vector<string: iterator it;//normal iterator vector<string>. const_iterator conit, / read only iterator vector<string>: reverse_ _iterator rit,//reverse iterator a USage for (it= svec begin; it != svec end it++) cout < *it cout < it->size(<< endl
Iterators (cont.) ◼ Definition vector<string> svec; vector<string>::iterator it; //normal iterator vector<string>::const_iterator conit; //read only iterator vector<string>::reverse_iterator rit; //reverse iterator ◼ Usage for (it = svec.begin(); it != svec.end(); it++) cout << *it << ‘ ’; cout << it->size() << endl;