ok i indented the code...and I also included the driver program - this should not be touched at all..
Code:
#ifndef GUARD_myalgorithm_h
#define GUARD_myalgorithm_h
template <class In, class Out>
Out my_copy(In b, In e, Out d)
{
while (b != e)
*d++ = *b++;
return d;
}
template <class In, class Out>
int my_accumulate(In b, In e, Out t)
{
int aaa = *t;
while (b != e) {
aaa = aaa + *b;
b++;
}
return aaa;
}
template <class In>
bool my_equal(In b, In e, In t)
{
return true;
}
template <class In>
void my_fill(In b, In e, In t)
{
while (b != e) {
*b = *t;
b++;
}
}
template <class In>
bool my_lexicographic_compare(In b, In e, In b2, In e2)
{
while (b != e && b2 != e2) {
for (b; b != e; b++)
if (*b >= *b2)
return false;
if (*b < *b2)
return true;
}
}
template <class In>
iter my_min_element(In b, In e)
{
return;
}
#endif
program:
Code:
int main()
{
// set up some vector and list objects to use
int a[] = {1, 2, 3, 4};
size_t size_a = sizeof(a)/sizeof(*a);
int b[] = {5, 6, 7};
size_t size_b = sizeof(b)/sizeof(*b);
int c[] = {4, 3, 2, 3, 4};
size_t size_c = sizeof(c)/sizeof(*c);
vector<int> va(a, a + size_a);
vector<int> vb(b, b + size_b);
vector<int> vc(c, c + size_c);
list<int> la(a, a + size_a);
list<int> lb(b, b + size_b);
// test my_accumulate
cout << "test my_accumulate:\n";
cout << " The sum of the elements in va is "
<< my_accumulate(va.begin(), va.end(), 0) << endl;
cout << " The sum of the elements in lb is "
<< my_accumulate(lb.begin(), lb.end(), 0) << endl;
// test my_equal
cout << "\ntest my_equal:\n";
if (my_equal(va.begin(), va.end(), vb.begin()))
cout << " va and vb are equal\n";
else
cout << " va and vb are not equal\n";
if (my_equal(va.begin(), va.end(), va.begin()))
cout << " va equals itself\n";
else
cout << " va and lb are not equal\n";
// test my_fill
cout << "\ntest my_fill:\n";
cout << " original va: " ;
my_copy(va.begin(), va.end(), ostream_it(cout, " "));
cout << endl;
my_fill(va.begin(), va.end(), 30);
cout << " modified va: ";
my_copy(va.begin(), va.end(), ostream_it(cout, " "));
cout << endl;
// test my_lexicographic_compare
cout << "\ntest my_lexicographic_compare:\n";
if (my_lexicographic_compare(la.begin(), la.end(), lb.begin(), lb.end()))
cout << " la is less than lb\n";
else
cout << " la is not less than lb\n";
if (my_lexicographic_compare(va.begin(), va.end(), vb.begin(), vb.end()))
cout << " va is less than vb\n";
else
cout << " va is not less than vb\n";
// test my_min_element
cout << "\ntest my_min_element:\n";
cout << " the smallest element of vc is "
<< *my_min_element(vc.begin(), vc.end())
<< endl;
return 0;
}
sorry i need to keep the my_(name) cuz the project description tells me to do it like that...