// This program includes several examples of using templates for generic // programming in C++. #include #include #include #include #include #include #include using namespace std; // This is a templated function that requires a type that overrides the + // operator and the << operator, as in int, double, string template void print_double(const T & value) { cout << value + value << endl; } // We introduced a new type that we wanted to use with our templates, which // required overloading various operators to allow the templates to work with a // point. struct point { point(int x, int y) : x(x), y(y) { // nothing else to do } int x, y; }; point operator+(const point & lhs, const point & rhs) { return point(lhs.x + rhs.x, lhs.y + rhs.y); } ostream & operator<<(ostream & out, const point & p) { out << "(" << p.x << ", " << p.y << ")"; return out; } double distance(const point & p) { return sqrt(p.x * p.x + p.y * p.y); } bool operator<(const point & lhs, const point & rhs) { return distance(lhs) < distance(rhs); } // This templated function operators on structured objects that have iterators // for begin() and end() and whose elements override the insertion operator // (<<). It returns a bracketed, comma-separated list of values in a string. template string to_string(T v) { ostringstream out; out << "["; auto itr = v.begin(); if (itr != v.end()) { out << *itr; itr++; while (itr != v.end()) { out << ", " << *itr; itr++; } } out << "]"; return out.str(); } // This templated function searches for the maximum value in a vector of // values. The values need to override the less-than operator. template E find_max(const vector & v) { if (v.empty()) { throw runtime_error("empty vector"); } int max_index = 0; for (int i = 1; i < v.size(); i++) { if (v[max_index] < v[i]) { max_index = i; } } return v[max_index]; } // this is a templated struct that allows you to store three values and to // access those values using the instance variables first, second, and third. template struct triple { triple(const T1 & x, const T2 & y, const T3 & z) : first(x), second(y), third(z) { // nothing else to do } T1 first; T2 second; T3 third; }; int main() { print_double(18); print_double(17.4); string s = "hello"; print_double(s); print_double(point(3, 5)); cout << endl; vector v1 {3, 18, 9, 24, 7, -8, 203, 15, 42}; vector v2 {3.4, 5.7, 19.8, 206.6, 195.2, -18.3}; vector v3 {"four", "score", "and", "seven", "years", "ago"}; vector v4; vector v5 {point(3, 5), point(8, 7), point(9, 9), point(12, 5)}; cout << "v1 max = " << find_max(v1) << endl; cout << "v2 max = " << find_max(v2) << endl; cout << "v3 max = " << find_max(v3) << endl; // cout << "v4 max = " << find_max(v4) << endl; cout << "v5 max = " << find_max(v5) << endl; cout << endl; cout << to_string(v1) << endl; set s1 {3, 8, 19, 7, 42, 3, 3, 3}; set s2 {"four", "score", "and", "seven", "years", "ago"}; cout << to_string(s1) << endl; cout << to_string(s2) << endl; triple t("hello", 18, 45.2); cout << t.first << " " << t.second << " " << t.third << endl; return 0; }