ITERATOR
template<class InputIterator,class T>
InputIterator find(InputIterator first,InputIterator last,const T& value)
{
while(first != last && *first != value)
++first;
return first;
}
代码示例
1 #include <iostream> 2 #include <vector> 3 #include <list> 4 #include <deque> 5 #include <algorithm> 6 #include <iostream> 7 8 using namespace std; 9 10 int main(int argc, char *argv[]) 11 { 12 const int arraySize = 7; 13 int ia[arraySize] = {0,1,2,3,4,5,6}; 14 15 vector<int> ivect(ia,ia+arraySize); 16 list<int> ilist(ia,ia+arraySize); 17 deque<int> ideque(ia,ia+arraySize); 18 19 vector<int>::iterator it1 = find(ivect.begin(),ivect.end(),4); 20 if(it1 == ivect.end()) 21 cout << "4 not found." << endl; 22 else 23 cout << "4 found. " << * it1 << endl; 24 25 list<int>::iterator it2 = find(ilist.begin(),ilist.end(),6); 26 if(it2 == ilist.end()) 27 cout << "6 not found. " << endl; 28 else 29 cout << "6 found. " << *it2 << endl; 30 31 deque<int>::iterator it3 = find(ideque.begin(),ideque.end(),8); 32 if(it3 == ideque.end()) 33 cout << "8 not found. " << endl; 34 else 35 cout << "8 find " << *it3 << endl; 36 37 38 return 0; 39 }
stl中容器有vector\set\list等等等等
算法有find\count等
两者独立 而他们之间的联系便是由iterator进行连接 将两者粘合起来
iterator类似智能指针
智能指针auto_ptr 除了拥有平常指针概念的功能 还具有引用计数功能
通过对该指针指向的元素的引用计数 自动释放元素内存资源 而不必手动调用delete
示例代码如下
c++ stl源码剖析学习笔记(二)iterator auto_ptr(老版本书籍示例 新版本C++中已经废除此概念)
原文:http://www.cnblogs.com/itdef/p/6971290.html