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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
#include <fstream>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
// globals as convenience for executing procedures
const int int_size = 12;
const int string_size = 4;
const int int_not_present = 1024;
int ia[int_size] =
{ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 };
string sa[string_size] =
{ "pooh", "piglet", "eeyore", "tigger" };
vector<int> ivec(ia, ia + int_size);
vector<string> svec(sa, sa + string_size);
template <typename elemType>
inline const elemType* begin( const vector<elemType>& vec )
{
return vec.empty() ? 0 : &vec[0];
}
template <typename elemType>
const elemType* find_ver4( const elemType* array, int size,
const elemType& value )
{
if (!array || size < 1)
return 0;
// ++array increments array by one element
for (int ix = 0; ix < size; ++ix, ++array)
// *array dereferences the address
if (*array == value)
return array;
return 0;
}
void prog_find_vers()
{
const int* iptr = find_ver4( begin( ivec ), ivec.size(), ivec[2] );
if (iptr != &ivec[2])
cerr << "?? find_ver4 failed with int vector!\n";
else
cerr << "!! find_ver4 ok with int vector!\n";
const string* sptr = find_ver4( begin( svec ), svec.size(), svec[2] );
if (sptr != &svec[2])
cerr << "?? find_ver4 failed with string vector!\n";
else
cerr << "!! find_ver4 ok with string vector!\n";
}
int main()
{
cerr << "About to execute prog_find_vers()\n\n";
prog_find_vers();
return 0; // quiet vc6++
}
|