9.8. Exercises
1: | Write a template reverse() function to reverse elements of a generic array in place. Can you use the template swap() function from page 391? Write a display() template function to show your results.
// reverse.c - reverse array elements in place
#include <iostream.h>
#include "String.h"
int main()
{
static int a[5] = { 12, 47, -33, 101, 56 };
reverse(a, 5);
display(a, 5);
static String b[4] = { "one", "two", "three", "four" };
reverse(b, 4);
display(b, 4);
return 0;
}
$ reverse
56 101 -33 47 12
four three two one
|
2: | Write a template move() function that copies arrays of generic objects for a specific count. Here are several examples of move() with String objects and integers.
String a[100], b[100]; // a and b are arrays of ... |
Get Navigating C++ and Object-Oriented Design now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.