ITERATOR
Template<class Inputiterator,class t>
Inputiterator Find (inputiterator first,inputiterator last,const t& value)
{
while (first! = Last && *first! = value)
++first;
return first;
}
code example
1#include <iostream>2#include <vector>3#include <list>4#include <deque>5#include <algorithm>6#include <iostream>7 8 using namespacestd;9 Ten intMainintargcChar*argv[]) One { A Const intArraySize =7; - intIa[arraysize] = {0,1,2,3,4,5,6}; - thevector<int> Ivect (ia,ia+arraySize); -list<int> IList (ia,ia+arraySize); -deque<int> Ideque (ia,ia+arraySize); - +vector<int>::iterator it1 = Find (Ivect.begin (), Ivect.end (),4); - if(It1 = =ivect.end ()) +cout <<"4 not found."<<Endl; A Else atcout <<"4 found."<< * It1 <<Endl; - -list<int>::iterator it2 = Find (Ilist.begin (), Ilist.end (),6); - if(It2 = =ilist.end ()) -cout <<"6 not found."<<Endl; - Else incout <<"6 found."<< *it2 <<Endl; - todeque<int>::iterator it3 = Find (Ideque.begin (), Ideque.end (),8); + if(It3 = =ideque.end ()) -cout <<"8 not found."<<Endl; the Else *cout <<"8 Find"<< *it3 <<Endl; $ Panax Notoginseng - return 0; the}
The container in the STL is vector\set\list and so on.
The algorithm has find\count and so on
The two are independent, and the connection between them is to be connected by iterator.
Iterator similar smart pointers
Smart Pointer auto_ptr has a reference counting function in addition to the usual pointer concept
Automatically frees element memory resources by reference count of the elements pointed to by the pointer without having to call delete manually
The sample code is as follows
C + + STL Source Analysis Learning Note (ii) iterator auto_ptr (old version of book example new version C + + has abolished this concept)