再请教一个模板问题,请问是什么错误。
题目要求:编写行为类似于标准库中的find算法的模板,模板接受一个类型形参,该形参指定函数形参(一对迭代器)的类型。使用函数在vector <int> 和vector <string> 中查找指定值。
我写的如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector <int> ivec;
vector <string> svec;
template <typename T1, typename T2>
int Find(const T1 &v, const T2 &val)
{
T1::iterator iter;
for (iter = v.begin(); iter != v.end(); iter++)
{
if (val == *iter)
{
cout < < val < < endl;
return 1;
}
}
return -1;
}
void main()
{
for (int i = 0; i < 5; i++)
ivec.push_back(i);
string string1 = "ab ";
string string2 = "abc ";
string string3 = "abcd ";
string string4 = "abcde ";
string string5 = "abcdef ";
svec.push_back(string1);
svec.push_back(string2);
svec.push_back(string3);
svec.push_back(string4);
svec.push_back(string5);
Find(ivec, 2);
}
[解决办法]
T1::iterator iter;
==>
T1::const_iterator iter;