#include <iostream> #include <vector> using namespace std; template<typename T1, typename T2> T1 findValue(T1 beg,T1 end, T2 val) { while (beg!=end) { if (*beg==val) { break; } else { ++beg; } } return beg; } int main_9_13() { int ia[]={0,1,2,3,4,5,6}; vector<int> ivec(ia,ia+7); //读入要找的数据 cout<<"Enter a integer:"<<endl; int ival; cin>>ival; //调用findInt函数查找ival vector<int>::iterator it; it=findValue(ivec.begin(),ivec.end(),ival);//对于函数模板,编译器会自动推断 if (it!=ivec.end()) { cout<<ival<<" is a element of vector."<<endl; } else { cout<<ival<<" is not a element of vector."<<endl; } return 0; }
C++ primer 习题练习:9.13
最新推荐文章于 2024-02-26 12:10:00 发布