1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
#include <string>
using namespace std;
//======================================================================
template<class T> int getIndex( T a[], int size, T valueToFind )
{
for ( int i = 0; i < size; i++ )
{
if ( a[i] == valueToFind ) return i; // if found, return index
}
return -1; // if not found, return an indication of that (-1)
}
//======================================================================
int main()
{
int iArray[] = { 10, 20, 30, 40 };
double dArray[] = { 13.4, 15.2, 12.0, -8.7 };
string sArray[] = { "Vauxhall", "Citroen", "Volkswagen", "Toyota", "Ford", "Volvo" };
int ival = 30 ; cout << "Value: " << ival << " Index: " << getIndex( iArray, 4, ival ) << endl;
double dval = 15.2 ; cout << "Value: " << dval << " Index: " << getIndex( dArray, 4, dval ) << endl;
string sval = "Ford"; cout << "Value: " << sval << " Index: " << getIndex( sArray, 6, sval ) << endl;
sval = "FFFF"; cout << "Value: " << sval << " Index: " << getIndex( sArray, 6, sval ) << endl;
}
|