templateT multiply(T x, T y) { return x*y; } int main() { int a = 5, b = 3; float c = 2.5, d = 1.5; cout << "Product of integers: " << multiply(a,b) << endl; cout << "Product of floats: " << multiply(c,d) << endl; return 0; }
vectornums = {1,2,3,4,5}; int sum = 0; for_each(nums.begin(),nums.end(),[&](int x){sum += x;}); cout << "Sum: " << sum << endl;
unique_ptrThis code demonstrates the use of unique_ptr, a type of smart pointer that represents unique ownership of an object. The pointer is initialized with a new integer (10) and its value is printed. The pointer is then reset with a new integer (20) and its new value is printed. The unique_ptr automatically deallocates the memory when it goes out of scope.p(new int(10)); cout << *p << endl; p.reset(new int(20)); cout << *p << endl;