cpp
exercises
exercises.cpp⚙️cpp
/**
* Function Templates - Exercises
* Compile: g++ -std=c++17 -Wall exercises.cpp -o exercises
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// ============================================================
// Exercise 1: Basic Template ⭐
// ============================================================
// TODO: Create template function 'square' that returns x * x
void exercise1() {
// cout << square(5) << endl; // 25
// cout << square(3.14) << endl; // 9.8596
cout << "(Implement square template)" << endl;
}
// ============================================================
// Exercise 2: Two Type Parameters ⭐⭐
// ============================================================
// TODO: Create template 'multiply' with two type parameters
// Should work with different types: multiply(2, 3.5) = 7.0
void exercise2() {
// cout << multiply(2, 3.5) << endl; // 7.0
// cout << multiply(3.5, 2) << endl; // 7.0
cout << "(Implement multiply template)" << endl;
}
// ============================================================
// Exercise 3: Array Functions ⭐⭐
// ============================================================
// TODO: Create template 'arraySum' that sums array elements
// Use non-type template parameter for size
void exercise3() {
int nums[] = {1, 2, 3, 4, 5};
double dbls[] = {1.5, 2.5, 3.5};
// cout << arraySum(nums) << endl; // 15
// cout << arraySum(dbls) << endl; // 7.5
cout << "(Implement arraySum template)" << endl;
}
// ============================================================
// Exercise 4: Find in Container ⭐⭐
// ============================================================
// TODO: Create template 'contains' that checks if value exists
// Works with any container that supports begin()/end()
void exercise4() {
vector<int> nums = {1, 2, 3, 4, 5};
vector<string> words = {"hello", "world"};
// cout << contains(nums, 3) << endl; // 1 (true)
// cout << contains(nums, 10) << endl; // 0 (false)
// cout << contains(words, "hello") << endl; // 1
cout << "(Implement contains template)" << endl;
}
// ============================================================
// Exercise 5: Template Specialization ⭐⭐⭐
// ============================================================
// TODO: Create template 'toString' that converts value to string
// Specialize for: int, double, bool, const char*
void exercise5() {
// cout << toString(42) << endl; // "42"
// cout << toString(3.14) << endl; // "3.14"
// cout << toString(true) << endl; // "true"
// cout << toString("hello") << endl; // "hello"
cout << "(Implement toString with specializations)" << endl;
}
// ============================================================
// Exercise 6: Comparator Template ⭐⭐
// ============================================================
// TODO: Create template 'findMax' that finds max element
// Takes container and comparison function
void exercise6() {
vector<int> nums = {3, 1, 4, 1, 5, 9};
vector<string> words = {"apple", "pie", "a"};
// auto maxNum = findMax(nums, [](int a, int b) { return a > b; });
// auto longest = findMax(words, [](const string& a, const string& b) {
// return a.length() > b.length();
// });
cout << "(Implement findMax template)" << endl;
}
// ============================================================
// Exercise 7: Transform Template ⭐⭐⭐
// ============================================================
// TODO: Create template 'transform' that applies function to each element
// Returns new container with transformed values
void exercise7() {
vector<int> nums = {1, 2, 3, 4, 5};
// auto doubled = transform(nums, [](int x) { return x * 2; });
// auto strings = transform(nums, [](int x) { return to_string(x); });
cout << "(Implement transform template)" << endl;
}
// ============================================================
// Exercise 8: Clamp Template ⭐⭐
// ============================================================
// TODO: Create template 'clamp' that restricts value to range
// clamp(value, min, max) returns min if value < min, max if value > max
void exercise8() {
// cout << clamp(5, 0, 10) << endl; // 5
// cout << clamp(-5, 0, 10) << endl; // 0
// cout << clamp(15, 0, 10) << endl; // 10
// cout << clamp(3.14, 0.0, 1.0) << endl; // 1.0
cout << "(Implement clamp template)" << endl;
}
// ============================================================
// MAIN
// ============================================================
int main() {
cout << "=== Function Templates Exercises ===" << endl;
cout << "\nEx1: Square" << endl;
exercise1();
cout << "\nEx2: Multiply" << endl;
exercise2();
cout << "\nEx3: Array Sum" << endl;
exercise3();
cout << "\nEx4: Contains" << endl;
exercise4();
cout << "\nEx5: ToString" << endl;
exercise5();
cout << "\nEx6: FindMax" << endl;
exercise6();
cout << "\nEx7: Transform" << endl;
exercise7();
cout << "\nEx8: Clamp" << endl;
exercise8();
return 0;
}
// ============================================================
// ANSWERS
// ============================================================
/*
Ex1:
template<typename T>
T square(T x) { return x * x; }
Ex2:
template<typename T, typename U>
auto multiply(T a, U b) { return a * b; }
Ex3:
template<typename T, size_t N>
T arraySum(const T (&arr)[N]) {
T sum = T();
for (size_t i = 0; i < N; ++i) sum += arr[i];
return sum;
}
Ex4:
template<typename Container, typename T>
bool contains(const Container& c, const T& value) {
for (const auto& item : c) {
if (item == value) return true;
}
return false;
}
Ex5:
template<typename T>
string toString(const T& value) { return to_string(value); }
template<> string toString<bool>(const bool& value) { return value ? "true" : "false"; }
template<> string toString<const char*>(const char* const& value) { return string(value); }
Ex6:
template<typename Container, typename Compare>
auto findMax(const Container& c, Compare comp) {
auto maxIt = c.begin();
for (auto it = c.begin(); it != c.end(); ++it) {
if (comp(*it, *maxIt)) maxIt = it;
}
return *maxIt;
}
Ex7:
template<typename Container, typename Func>
auto transform(const Container& c, Func f) {
vector<decltype(f(*c.begin()))> result;
for (const auto& item : c) result.push_back(f(item));
return result;
}
Ex8:
template<typename T>
T clamp(T value, T minVal, T maxVal) {
if (value < minVal) return minVal;
if (value > maxVal) return maxVal;
return value;
}
*/