vector由于key是順序增長的整數,是以無法提供高效的按value查找的函數。使用vector進行查找時,要借助find.
vector<int>::iterator p;
p=find(v.begin(),v.end(),a);
if(p!=v.end()) cout<<"we find the number: "<<*p<<endl;
else cout<<"can not find"<<endl;
find可用于其它任何的查找:
int array[10]={10,20,30,40};
int *pp=find(array,array+4,20);
cout<<*pp<<endl;