64,674
社区成员
发帖
与我相关
我的任务
分享
完整的调用~~!
#include <iostream>
#include <vector>
using namespace std;
template <class T>
typename T::iterator
Find_value( typename T::iterator first,const typename T::iterator last,const typename T::value_type value)
{
while(first!=last)
{
if(*first==value)
{
return first;
}
first++;
}
return last;
}
int main()
{
vector<int> test;
for(int i=0;i<10;++i)
{
test.push_back(i);
}
vector<int>::iterator pos=Find_value<vector<int> >(test.begin(),test.end(),5);
cout<<*pos<<endl;
return 0;
}
#include <iostream.h>
#include <vector>
using namespace std;
vector<int>::iterator Find_value( vector<int>::iterator first,const vector<int>::iterator last,int value)
{
while(first!=last)
{
if(*first==value)
{
return first;
}
first++;
}
return last;
}
void main()
{
vector<int> ivec(10);
vector<int>::iterator begin=ivec.begin();
vector<int>::iterator end=ivec.end();
vector<int>::iterator val;
val=Find_value(begin,end,0);
cout<<*val<<endl;
}
2楼的代码有点问题,参看下面的
template <class T>
typename T::iterator
Find_value( typename T::iterator first,const typename T::iterator last,int value)
{
while(first!=last)
{
if(*first==value)
{
return first;
}
first++;
}
return last;
}
有虚心求教的心态总是好的,没什么丢人的,参看如下代码
template <class T>
typename T::iterator
Find_value(const typename T::iterator first,const typename T::iterator last,int value)
{
while(first++!=last)
{
if(*first==value)
{
return first;
}
}
return first;
}