basic program need help understanding!

#include <iostream>

using namespace std;

template <class T>
int search (T *data, int size, T value)
{
for (int i =0; i < size; i++)
if (data[i] == value)
return i;

return -1;
}

int main()
{
const int size = 4;
int data[size] = {0,1,2,3};
int value = data[size - 1];

cout << search(data, size, value) << endl;

}


could someone explain this to me step by step?
It's a template that searches an array of data for a specific value, and returns that values location within the array. Type t can be any one type, at any point in time. In this case, it is an int, it is looking for int 3 in int *data, and returns it's position (3).

Look up templates for more info. The main point of templates is that this code will work if you have an array of ints, doubles, bools, or puppies.
i think im just confused about the

int data[size] = {0,1,2,3};
int value = data[size - 1];

data[3] means it is looking at 3 correct? since data[0] = 0 (the first location), data[1] = 1, ...etc

data[size] or data[4] has 5 memory slots?
data[4] has 4 values, 0-3.

int data[size] = {0,1,2,3}; declares an array of ints with a size of... size (which is equal to 4 in this case), and initializes those values to 0-3, so that data[x] == x You don't always have to make the array location equal to the value (that would be kinda redundant), but in this case, the author did choose to.
oh okay I see thanks for your help!!
wait actually Im confused on the T *data parameter. I cant seem to see how it is used in the function search.
template <class T> says that the following template will take 1 data type that is unknown to the programmer. It is referred to as class T. It doesn't matter what T is, but it has to be one thing, so int search (T *data, int size, T value) could be (int, int, int), or it could be (float, int, float), or (puppy, int, puppy).
#include <iostream>

using namespace std;

class Exception {};
class IOException : public Exception {};
class MemException : public Exception {};

int main()
{
for (int i = 0; i<3; i++)
{
try
{
switch(i)
{
case 0:
throw Exception();
case 1:
throw IOException();
break;
case 2:
throw MemException();
break;
}
}

catch (Exception e)
{
cout << "E";
}
catch (MemException e)
{
cout << "M";
}
catch (IOException e)
{
cout << "I";
}
}
cout << endl;
}


cant seem to see why the output is EEE


Both of the exceptions are derived from the base exception class, the first catch statement tries its hand at catching what is thrown, and because it can catch everything, it catches everything. If you were to put the first catch clause at the end of the program, you would see more interesting things.
i understand the first catch to output E

but what about the 2nd catch statement?

will it output EI?

then 3rd EM?

since the the for loop updates till i = 2
wait what am i thinking there arent any contructors slkdjfaslkdjf

i mean EMI
Each time, no matter which type of exception it throws, it is, or is a derived class of exception, so exception can catch them all
#include <iostream>

using namespace std;

class Exception {};
class IOException : public Exception {};
class MemException : public Exception {};

int main()
{
for (int i = 0; i<3; i++)
{
try
{
switch(i)
{
case 0:
throw Exception();
case 1:
throw IOException();
break;
case 2:
throw MemException();
break;
}
}

catch (MemException e)
{
cout << "M";
}
catch (IOException e)
{
cout << "I";
}
catch (Exception e)
{
cout << "E";
}
}
cout << endl;
}

but this code outputs EIM why not EEE if exception catches all of them?
I believe Exception is the parent and MemException, IOException are sub-classes ? So usually for exception handling it is to catch the most specific and if there isn't any, it moves "up" the hierarchy until it find the parent of all and that is Exception.
Topic archived. No new replies allowed.