编译并运行我的代码;如果您使用带小数值的整数或浮点数,则比较结果。为什么有区别?
这是我的代码:
#include <iostream>
using namespace std;
template<class T>
T find(T array[], T len, T num){
for (int i = 0; i < len; ++i){
if (array[i] == num)
return i;
}
return -1;
}
int main () {
int array1[5] = { 4, 7, 3, 5, 6 }, num1;
float array2[5] ={121.2, 111.5, 300.1, 500.1, 600.1 }, num2;
cout << "Enter an int:" << " " ;
cin >> num1;
cout << "Enter a float:" << " " ;
cin >> num2;
int x = find<int>(array1,5,num1);
float y= find<float>(array2,5,num2);
cout << "The index for the int is:" << " " << x << endl;
cout << "The index for the float is:" << " " << y << endl;
return 0;
}
使用整数和使用浮点数时,我找不到这两个结果之间的区别。